Share via


SecurityAction Výčet

Definice

Upozornění

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

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.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
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 aktuálním objektem oprávnění je volajícím odepřena, a to i v případě, že jim bylo uděleno oprávnění pro přístup k němu (viz Použití metody Odepřít).

InheritanceDemand 7

Odvozená třída, která dědí třídu nebo přepsání metody, 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. SecurityCriticalAttribute Pro částečnou důvěryhodnost použijte Demand.

PermitOnly 5

Přístup je možné pouze k prostředkům určeným tímto 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

Požadavek na minimální oprávnění vyžadovaná 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é spustit). Tento požadavek implicitně odmítne všechna ostatní oprávnění, která nejsou konkrétně 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 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 zastaralé ve všech verzích rozhraní .NET Framework a .NET. Nedávné verze rozhraní .NET nedodržují poznámky CAS a při použití rozhraní API souvisejících s cas vytvářejí 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í žádostí o oprávnění Deny, RequestMinimum, RequestOptional a RequestRefuse. Tyto požadavky by se neměly používat v kódu, který je založený na rozhraní .NET Framework 4 nebo novějším. Další informace o těchto 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í využ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 Čas načtení 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