Udostępnij za pośrednictwem


SecurityAction Wyliczenie

Definicja

Przestroga

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

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.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 = 
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 osoby wywołujące wyższe w stosie nie otrzymały uprawnień dostępu do zasobu (zobacz Używanie metody assert).

Demand 2

Wszystkie osoby 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 dla osób wywołujących, 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 zastępowanie metody 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 programie .NET Framework 4. Aby uzyskać pełną relację zaufania, należy zamiast tego użyć SecurityCriticalAttribute polecenia w przypadku częściowego zaufania.Demand

PermitOnly 5

Dostęp do zasobów określonych przez ten obiekt uprawnień można uzyskać, nawet jeśli kod otrzymał uprawnienie 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 specjalnie. Tej akcji można używać tylko w zakresie zestawu.

RequestRefuse 10

Żądanie, że uprawnienia, które mogą być nieprawidłowe, nie zostaną przyznane kodowi 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 ma tylko IsolatedStoragePermissionwartość , a także pokazuje, jak zapisywać i odczytywać dane 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 do kodu (CAS) zostały przestarzałe we wszystkich wersjach programu .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, w jaki odbywa się każda akcja zabezpieczeń, oraz obsługiwane przez nią elementy docelowe.

Ważne

W programie .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 programie .NET Framework 4 lub nowszym. Aby uzyskać więcej informacji na temat tych i innych zmian, zobacz Zmiany zabezpieczeń.

Nie należy używać w LinkDemand programie .NET Framework 4. Zamiast tego użyj polecenia SecurityCriticalAttribute , aby ograniczyć użycie do w pełni zaufanych aplikacji lub użyć Demand go, aby ograniczyć częściowo zaufane osoby wywołujące.

Deklaracja akcji zabezpieczeń Czas akcji Obsługiwane elementy docelowe
LinkDemand (nie używaj w programie .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 programie .NET Framework 4) W czasie wykonywania Klasa, metoda
PermitOnly W czasie wykonywania Klasa, metoda
RequestMinimum (przestarzałe w programie .NET Framework 4) Czas udzielania Zestaw
RequestOptional (przestarzałe w programie .NET Framework 4) Czas udzielania Zestaw
RequestRefuse (przestarzałe w programie .NET Framework 4) Czas udzielania Zestaw

Aby uzyskać dodatkowe informacje na temat obiektów docelowych atrybutów, zobacz Attribute.

Dotyczy