SecurityAction Enumerazione

Definizione

Attenzione

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

Attenzione

CAS support is not available with Silverlight applications.

Specifica le azioni relative alla sicurezza che possono essere eseguite con la sicurezza dichiarativa.

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
Ereditarietà
SecurityAction
Attributi

Campi

Assert 3

Il codice chiamante consente di accedere alla risorsa identificata dall'oggetto di autorizzazione corrente, anche se i primi chiamanti dello stack non sono autorizzati ad accedere alla risorsa (vedere Uso del metodo Assert).

Demand 2

A tutti i chiamanti nella parte superiore dello stack di chiamate deve essere concessa l'autorizzazione specificata dall'oggetto di autorizzazione corrente.

Deny 4

Ai chiamanti non viene consentito di accedere alla risorsa specificata dall'oggetto di autorizzazione corrente, anche se sono autorizzati ad accedere alla risorsa (vedere Uso del metodo Deny).

InheritanceDemand 7

La classe derivata che eredita la classe o con cui un metodo viene sottoposto a overriding deve disporre dell'autorizzazione specificata.

LinkDemand 6

È necessario concedere al chiamante diretto l'autorizzazione specificata. Non usare in .NET Framework 4. Per un'attendibilità totale, usare invece SecurityCriticalAttribute; per un'attendibilità parziale, usare Demand.

PermitOnly 5

È possibile accedere solo alle risorse specificate dall'oggetto di autorizzazione, anche se al codice è stata concessa l'autorizzazione per accedere ad altre risorse.

RequestMinimum 8

Richiesta delle autorizzazioni minime necessarie per l'esecuzione del codice. Questa azione può essere usata solo nell'ambito dell'assembly.

RequestOptional 9

Richiesta di autorizzazioni aggiuntive facoltative (non necessarie per l'esecuzione). Con questa richiesta viene implicitamente rifiutata ogni altra autorizzazione che non sia stata esplicitamente richiesta. Questa azione può essere usata solo nell'ambito dell'assembly.

RequestRefuse 10

Richiesta per impedire che al codice chiamante vengano concesse autorizzazioni utilizzabili in modo improprio. Questa azione può essere usata solo nell'ambito dell'assembly.

Esempio

In questo esempio viene illustrato come notificare a CLR che il codice nei metodi chiamati ha solo IsolatedStoragePermissione illustra anche come scrivere e leggere dall'archiviazione isolata.

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.

Commenti

Attenzione

La sicurezza dall'accesso al codice è stata deprecata in tutte le versioni di .NET Framework e .NET. Le versioni recenti di .NET non rispettano le annotazioni CAS e generano errori se vengono usate API correlate a CAS. Gli sviluppatori devono cercare metodi alternativi per eseguire attività di sicurezza.

La tabella seguente descrive il tempo in cui viene eseguita ogni azione di sicurezza e le destinazioni supportate.

Importante

In .NET Framework 4 il supporto del runtime è stato rimosso per l'applicazione delle richieste di autorizzazione Deny, RequestMinimum, RequestOptional e RequestRefuse. Queste richieste non devono essere usate nel codice basato su .NET Framework 4 o versione successiva. Per altre informazioni su questa e altre modifiche, vedere Modifiche alla sicurezza.

Non è consigliabile usare LinkDemand in .NET Framework 4. Usare invece per limitare l'utilizzo SecurityCriticalAttribute alle applicazioni completamente attendibili o per Demand limitare i chiamanti parzialmente attendibili.

Dichiarazione dell'azione di sicurezza Ora dell'azione Destinazioni supportate
LinkDemand (non usare in .NET Framework 4) Compilazione JITE Classe, metodo
InheritanceDemand Tempo di caricamento Classe, metodo
Demand Fase di esecuzione Classe, metodo
Assert Fase di esecuzione Classe, metodo
Deny (obsoleto in .NET Framework 4) Fase di esecuzione Classe, metodo
PermitOnly Fase di esecuzione Classe, metodo
RequestMinimum (obsoleto in .NET Framework 4) Tempo di concessione Assembly
RequestOptional (obsoleto in .NET Framework 4) Tempo di concessione Assembly
RequestRefuse (obsoleto in .NET Framework 4) Tempo di concessione Assembly

Per altre informazioni sulle destinazioni degli attributi, vedere Attribute.

Si applica a