Share via


SecurityAction Enum

Definisi

Perhatian

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

Menentukan tindakan keamanan yang dapat dilakukan menggunakan keamanan deklaratif.

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
Warisan
SecurityAction
Atribut

Bidang

Assert 3

Kode panggilan dapat mengakses sumber daya yang diidentifikasi oleh objek izin saat ini, bahkan jika penelepon yang lebih tinggi di tumpukan belum diberikan izin untuk mengakses sumber daya (lihat Menggunakan Metode Pernyataan).

Demand 2

Semua pemanggil yang lebih tinggi dalam tumpukan panggilan harus diberi izin yang ditentukan oleh objek izin saat ini.

Deny 4

Kemampuan untuk mengakses sumber daya yang ditentukan oleh objek izin saat ini ditolak oleh pemanggil, bahkan jika mereka telah diberikan izin untuk mengaksesnya (lihat Menggunakan Metode Tolak).

InheritanceDemand 7

Kelas turunan yang mewarisi kelas atau mengesampingkan metode diperlukan untuk diberikan izin yang ditentukan.

LinkDemand 6

Pemanggil langsung harus diberi izin yang ditentukan. Jangan gunakan dalam .NET Framework 4. Untuk kepercayaan penuh, gunakan SecurityCriticalAttribute sebagai gantinya; untuk kepercayaan parsial, gunakan Demand.

PermitOnly 5

Hanya sumber daya yang ditentukan oleh objek izin ini yang dapat diakses, meskipun kode telah diberi izin untuk mengakses sumber daya lain.

RequestMinimum 8

Permintaan izin minimum yang diperlukan agar kode dapat dijalankan. Tindakan ini hanya dapat digunakan dalam lingkup assembly.

RequestOptional 9

Permintaan izin tambahan yang bersifat opsional (tidak diperlukan untuk dijalankan). Permintaan ini secara implisit menolak semua izin lain yang tidak diminta secara khusus. Tindakan ini hanya dapat digunakan dalam lingkup assembly.

RequestRefuse 10

Permintaan agar izin yang mungkin disalahgunakan tidak akan diberikan ke kode panggilan. Tindakan ini hanya dapat digunakan dalam lingkup assembly.

Contoh

Contoh ini menunjukkan cara memberi tahu CLR bahwa kode dalam metode yang disebut hanya IsolatedStoragePermissionmemiliki , dan juga menunjukkan cara menulis dan membaca dari penyimpanan yang terisolasi.

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.

Keterangan

Perhatian

Keamanan Akses Kode (CAS) tidak digunakan lagi di semua versi .NET Framework dan .NET. Versi terbaru .NET tidak mematuhi anotasi CAS dan menghasilkan kesalahan jika API terkait CAS digunakan. Pengembang harus mencari cara alternatif untuk menyelesaikan tugas keamanan.

Tabel berikut ini menjelaskan waktu terjadinya setiap tindakan keamanan dan target yang didukungnya.

Penting

Di .NET Framework 4, dukungan runtime telah dihapus untuk memberlakukan permintaan izin Tolak, RequestMinimum, RequestOptional, dan RequestRefuse. Permintaan ini tidak boleh digunakan dalam kode yang didasarkan pada .NET Framework 4 atau yang lebih baru. Untuk informasi selengkapnya tentang ini dan perubahan lainnya, lihat Perubahan Keamanan.

Anda tidak boleh menggunakan LinkDemand di .NET Framework 4. Sebagai gantinya SecurityCriticalAttribute , gunakan untuk membatasi penggunaan ke aplikasi yang sepenuhnya tepercaya, atau gunakan Demand untuk membatasi sebagian penelepon tepercaya.

Deklarasi tindakan keamanan Waktu tindakan Target yang didukung
LinkDemand(jangan gunakan dalam .NET Framework 4) Kompilasi just-in-time Kelas, metode
InheritanceDemand Waktu muat Kelas, metode
Demand Durasi Kelas, metode
Assert Durasi Kelas, metode
Deny(usang di .NET Framework 4) Durasi Kelas, metode
PermitOnly Durasi Kelas, metode
RequestMinimum(usang di .NET Framework 4) Waktu pemberian Rakitan
RequestOptional(usang di .NET Framework 4) Waktu pemberian Rakitan
RequestRefuse(usang di .NET Framework 4) Waktu pemberian Rakitan

Untuk informasi tambahan tentang target atribut, lihat Attribute.

Berlaku untuk