SecurityAction Wyliczenie

Definicja

Przestroga

Code Access Security is not supported or honored by the runtime.

Przestroga

CAS support is not available with Silverlight applications.

Określa akcje zabezpieczeń, które można wykonać przy użyciu zabezpieczeń deklaratywnych.

public enum class SecurityAction
[System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public enum SecurityAction
public enum SecurityAction
[System.Serializable]
public enum SecurityAction
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum SecurityAction
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Obsolete("CAS support is not available with Silverlight applications.")]
public enum SecurityAction
[<System.Obsolete("Code Access Security is not supported or honored by the runtime.", DiagnosticId="SYSLIB0003", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
type SecurityAction = 
type SecurityAction = 
[<System.Serializable>]
type SecurityAction = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SecurityAction = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Obsolete("CAS support is not available with Silverlight applications.")>]
type SecurityAction = 
Public Enum SecurityAction
Dziedziczenie
SecurityAction
Atrybuty

Pola

Assert 3

Kod wywołujący może uzyskać dostęp do zasobu zidentyfikowany przez bieżący obiekt uprawnień, nawet jeśli obiekty wywołujące wyższe w stosie nie otrzymały uprawnień dostępu do zasobu (zobacz Używanie metody Assert).

Demand 2

Wszystkie obiekty wywołujące wyższe w stosie wywołań muszą mieć przyznane uprawnienie określone przez bieżący obiekt uprawnień.

Deny 4

Możliwość uzyskania dostępu do zasobu określonego przez bieżący obiekt uprawnień jest odrzucana obiektom wywołującym, nawet jeśli udzielono im uprawnień dostępu do niego (zobacz Using the Deny Method (Używanie metody odmowy).

InheritanceDemand 7

Klasa pochodna dziedziczące klasę lub przesłaniająca metodę jest wymagana do udzielenia określonego uprawnienia.

LinkDemand 6

Do udzielenia określonego uprawnienia jest wymagane natychmiastowe wywołanie. Nie należy używać w .NET Framework 4. W przypadku pełnej relacji zaufania użyj zamiast tego polecenia; w przypadku częściowego zaufania użyj SecurityCriticalAttribute polecenia Demand.

PermitOnly 5

Dostęp do zasobów określonych przez ten obiekt uprawnień można uzyskać tylko wtedy, gdy kod otrzymał uprawnienia dostępu do innych zasobów.

RequestMinimum 8

Żądanie dotyczące minimalnych uprawnień wymaganych do uruchomienia kodu. Tej akcji można używać tylko w zakresie zestawu.

RequestOptional 9

Żądanie dodatkowych uprawnień, które są opcjonalne (nie jest wymagane do uruchomienia). To żądanie niejawnie odrzuca wszystkie inne uprawnienia, których nie zażądano. Tej akcji można używać tylko w zakresie zestawu.

RequestRefuse 10

Żądanie, że uprawnienia, które mogą być nadużywane, nie zostaną przyznane do kodu wywołującego. Tej akcji można używać tylko w zakresie zestawu.

Przykłady

W tym przykładzie pokazano, jak powiadomić clR, że kod w wywoływanych metodach zawiera tylko IsolatedStoragePermissionmetodę , a także pokazuje, jak zapisywać i odczytywać z izolowanego magazynu.

using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::IO::IsolatedStorage;
using namespace System::IO;


static void WriteIsolatedStorage()
{
    try
    {
        // Attempt to create a storage file that is isolated by
        // user and assembly. IsolatedStorageFilePermission
        // granted to the attribute at the top of this file
        // allows CLR to load this assembly and execution of this
        // statement.
        Stream^ fileCreateStream = gcnew
            IsolatedStorageFileStream(
            "AssemblyData",
            FileMode::Create,
            IsolatedStorageFile::GetUserStoreForAssembly());

        StreamWriter^ streamWriter = gcnew StreamWriter(
            fileCreateStream);
        try
        {
            // Write some data out to the isolated file.

            streamWriter->Write("This is some test data.");
            streamWriter->Close();	
        }
        finally
        {
            delete fileCreateStream;
            delete streamWriter;
        } 
    }
    catch (IOException^ ex)
    {
        Console::WriteLine(ex->Message);
    }

    try
    {
        Stream^ fileOpenStream =
            gcnew IsolatedStorageFileStream(
            "AssemblyData",
            FileMode::Open,
            IsolatedStorageFile::GetUserStoreForAssembly());
        // Attempt to open the file that was previously created.

        StreamReader^ streamReader = gcnew StreamReader(
            fileOpenStream);
        try
        { 
            // Read the data from the file and display it.

            Console::WriteLine(streamReader->ReadLine());
            streamReader->Close();
        }
        finally
        {
            delete fileOpenStream;
            delete streamReader;
        }
    }
    catch (FileNotFoundException^ ex)
    {
        Console::WriteLine(ex->Message);
    }
    catch (IOException^ ex)
    {
        Console::WriteLine(ex->Message);
    }
}
// Notify the CLR to only grant IsolatedStorageFilePermission to called methods. 
// This restricts the called methods to working only with storage files that are isolated 
// by user and assembly.
[IsolatedStorageFilePermission(SecurityAction::PermitOnly, UsageAllowed = IsolatedStorageContainment::AssemblyIsolationByUser)]
int main()
{
    WriteIsolatedStorage();
}

// This code produces the following output.
//
//  This is some test data.
using System;
using System.Security.Permissions;
using System.IO.IsolatedStorage;
using System.IO;

// Notify the CLR to only grant IsolatedStorageFilePermission to called methods.
// This restricts the called methods to working only with storage files that are isolated
// by user and assembly.
[IsolatedStorageFilePermission(SecurityAction.PermitOnly, UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser)]
public sealed class App
{
    static void Main()
    {
        WriteIsolatedStorage();
    }
    private static void WriteIsolatedStorage()
    {
        // Attempt to create a storage file that is isolated by user and assembly.
        // IsolatedStorageFilePermission granted to the attribute at the top of this file
        // allows CLR to load this assembly and execution of this statement.
        using (Stream s = new IsolatedStorageFileStream("AssemblyData", FileMode.Create, IsolatedStorageFile.GetUserStoreForAssembly()))
        {

            // Write some data out to the isolated file.
            using (StreamWriter sw = new StreamWriter(s))
            {
                sw.Write("This is some test data.");
            }
        }

        // Attempt to open the file that was previously created.
        using (Stream s = new IsolatedStorageFileStream("AssemblyData", FileMode.Open, IsolatedStorageFile.GetUserStoreForAssembly()))
        {
            // Read the data from the file and display it.
            using (StreamReader sr = new StreamReader(s))
            {
                Console.WriteLine(sr.ReadLine());
            }
        }
    }
}

// This code produces the following output.
//
//  Some test data.
Option Strict On
Imports System.Security.Permissions
Imports System.IO.IsolatedStorage
Imports System.IO


' Notify the CLR to only grant IsolatedStorageFilePermission to called methods. 
' This restricts the called methods to working only with storage files that are isolated 
' by user and assembly.
<IsolatedStorageFilePermission(SecurityAction.PermitOnly, UsageAllowed:=IsolatedStorageContainment.AssemblyIsolationByUser)> _
Public NotInheritable Class App

    Shared Sub Main()
        WriteIsolatedStorage()
    End Sub
    Shared Sub WriteIsolatedStorage()
        ' Attempt to create a storage file that is isolated by user and assembly.
        ' IsolatedStorageFilePermission granted to the attribute at the top of this file 
        ' allows CLR to load this assembly and execution of this statement.
        Dim s As New IsolatedStorageFileStream("AssemblyData", FileMode.Create, IsolatedStorageFile.GetUserStoreForAssembly())
        Try

            ' Write some data out to the isolated file.
            Dim sw As New StreamWriter(s)
            Try
                sw.Write("This is some test data.")
            Finally
                sw.Dispose()
            End Try
        Finally
            s.Dispose()
        End Try

        ' Attempt to open the file that was previously created.
        Dim t As New IsolatedStorageFileStream("AssemblyData", FileMode.Open, IsolatedStorageFile.GetUserStoreForAssembly())
        Try
            ' Read the data from the file and display it.
            Dim sr As New StreamReader(t)
            Try
                Console.WriteLine(sr.ReadLine())
            Finally
                sr.Dispose()
            End Try
        Finally
            t.Dispose()
        End Try

    End Sub
End Class

' This code produces the following output.
'
'  Some test data.

Uwagi

Przestroga

Zabezpieczenia dostępu kodu (CAS) zostały uznane za przestarzałe we wszystkich wersjach .NET Framework i .NET. Najnowsze wersje platformy .NET nie honorują adnotacji CAS i generują błędy, jeśli są używane interfejsy API związane z usługą CAS. Deweloperzy powinni szukać alternatywnych sposobów wykonywania zadań zabezpieczeń.

W poniższej tabeli opisano czas wykonywania każdej akcji zabezpieczeń oraz obsługiwane przez nią elementy docelowe.

Ważne

W .NET Framework 4 obsługa środowiska uruchomieniowego została usunięta w celu wymuszania żądań uprawnień Deny, RequestMinimum, RequestOptional i RequestRefuse. Te żądania nie powinny być używane w kodzie opartym na .NET Framework 4 lub nowszym. Aby uzyskać więcej informacji o tym i innych zmianach, zobacz Zmiany zabezpieczeń.

Nie należy używać LinkDemand w .NET Framework 4. Zamiast tego użyj polecenia SecurityCriticalAttribute , aby ograniczyć użycie do w pełni zaufanych aplikacji lub użyć Demand polecenia w celu ograniczenia częściowo zaufanych wywołań.

Deklaracja akcji zabezpieczeń Czas akcji Obsługiwane elementy docelowe
LinkDemand(nie należy używać w .NET Framework 4) Kompilacja just in time Klasa, metoda
InheritanceDemand Czas ładowania Klasa, metoda
Demand W czasie wykonywania Klasa, metoda
Assert W czasie wykonywania Klasa, metoda
Deny(przestarzałe w .NET Framework 4) W czasie wykonywania Klasa, metoda
PermitOnly W czasie wykonywania Klasa, metoda
RequestMinimum(przestarzałe w .NET Framework 4) Czas udzielania Zestaw
RequestOptional(przestarzałe w .NET Framework 4) Czas udzielania Zestaw
RequestRefuse(przestarzałe w .NET Framework 4) Czas udzielania Zestaw

Aby uzyskać dodatkowe informacje o obiektach docelowych atrybutów, zobacz Attribute.

Dotyczy