SecurityAction Výčet

Definice

Upozornění

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

Upozornění

CAS support is not available with Silverlight applications.

Určuje akce zabezpečení, které lze provádět pomocí deklarativního zabezpečení.

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
Dědičnost
SecurityAction
Atributy

Pole

Assert 3

Volající kód má přístup k prostředku identifikovanému objektem aktuálního oprávnění, a to i v případě, že volajícím výše v zásobníku nebylo uděleno oprávnění pro přístup k prostředku (viz Použití metody Assert).

Demand 2

Všichni volající, kteří jsou výše v zásobníku volání, musí mít udělené oprávnění určené aktuálním objektem oprávnění.

Deny 4

Možnost přístupu k prostředku určenému objektem aktuálního oprávnění je volajícím odepřena, i když jim bylo uděleno oprávnění k přístupu (viz Použití metody Odepření).

InheritanceDemand 7

Odvozená třída, která dědí třídu nebo přepisuje metodu, musí mít udělené zadané oprávnění.

LinkDemand 6

Okamžitému volajícímu se vyžaduje udělení zadaného oprávnění. Nepoužívejte v rozhraní .NET Framework 4. Pro úplný vztah důvěryhodnosti použijte místo toho příkaz SecurityCriticalAttribute , pro částečnou důvěryhodnost použijte Demand.

PermitOnly 5

Přístup lze získat pouze k prostředkům určeným objektem oprávnění, a to i v případě, že kódu bylo uděleno oprávnění pro přístup k jiným prostředkům.

RequestMinimum 8

Žádost o minimální oprávnění potřebná ke spuštění kódu. Tuto akci lze použít pouze v rámci rozsahu sestavení.

RequestOptional 9

Žádost o další oprávnění, která jsou volitelná (není nutné ke spuštění). Tento požadavek implicitně odmítne všechna ostatní oprávnění, která nejsou výslovně požadována. Tuto akci lze použít pouze v rámci rozsahu sestavení.

RequestRefuse 10

Požadavek, aby oprávnění, která by mohla být zneužita, nebudou udělena volajícímu kódu. Tuto akci lze použít pouze v rámci rozsahu sestavení.

Příklady

Tento příklad ukazuje, jak upozornit CLR, že kód ve volaných metodách má pouze IsolatedStoragePermission, a také ukazuje, jak zapisovat a číst z izolovaného úložiště.

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.

Poznámky

Upozornění

Zabezpečení přístupu kódu (CAS) bylo ve všech verzích rozhraní .NET Framework a .NET zastaralé. Nedávné verze rozhraní .NET nedodržují poznámky CAS a při použití rozhraní API souvisejících s cas generují chyby. Vývojáři by měli hledat alternativní způsoby provádění úloh zabezpečení.

Následující tabulka popisuje čas provedení každé akce zabezpečení a cíle, které tato akce podporuje.

Důležité

V rozhraní .NET Framework 4 byla odebrána podpora modulu runtime pro vynucování požadavků na oprávnění Deny, RequestMinimum, RequestOptional a RequestRefuse. Tyto požadavky by se neměly používat v kódu založeném na rozhraní .NET Framework 4 nebo novějším. Další informace o tomto a dalších změnách najdete v tématu Změny zabezpečení.

V rozhraní .NET Framework 4 byste neměli používat LinkDemand . Místo toho použijte SecurityCriticalAttribute k omezení použití na plně důvěryhodné aplikace nebo použijte Demand k omezení částečně důvěryhodných volajících.

Prohlášení o bezpečnostní akci Čas akce Podporované cíle
LinkDemand (nepoužívejte v rozhraní .NET Framework 4) Kompilace za běhu Třída, metoda
InheritanceDemand Doba načítání Třída, metoda
Demand Za běhu Třída, metoda
Assert Za běhu Třída, metoda
Deny (zastaralé v rozhraní .NET Framework 4) Za běhu Třída, metoda
PermitOnly Za běhu Třída, metoda
RequestMinimum (zastaralé v rozhraní .NET Framework 4) Čas udělení Sestavení
RequestOptional (zastaralé v rozhraní .NET Framework 4) Čas udělení Sestavení
RequestRefuse (zastaralé v rozhraní .NET Framework 4) Čas udělení Sestavení

Další informace o cílech atributů najdete v tématu Attribute.

Platí pro