SecurityAction 列挙型

定義

注意事項

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

注意事項

CAS support is not available with Silverlight applications.

宣言型セキュリティを使用して実行できるセキュリティ アクションを指定します。

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
継承
SecurityAction
属性

フィールド

Assert 3

履歴内の上位の呼び出し元がリソースへのアクセス許可を付与されていない場合であっても、呼び出し元のコードは現在のアクセス許可オブジェクトで識別されるリソースにアクセスできます (Assert メソッドの使用を参照)。

Demand 2

呼び出し履歴の上位にあるすべての呼び出し元には、現在のアクセス許可オブジェクトで指定されたアクセス許可が付与されている必要があります。

Deny 4

リソースへのアクセス許可を付与されている場合であっても、現在のアクセス許可オブジェクトによって指定されるリソースにアクセスする能力は、呼び出し元に対して拒否されます (Deny メソッドの使用を参照)。

InheritanceDemand 7

クラスを継承する派生クラスやメソッドをオーバーライドする派生クラスに、指定したアクセス許可が与えられている必要があります。

LinkDemand 6

直接の呼び出し元には、指定したアクセス許可が付与されている必要があります。 .NET Framework 4 では を使用しないでください。 完全な信頼の場合、SecurityCriticalAttribute を使用します。部分的な信頼の場合は、代わりに Demand を使用します。

PermitOnly 5

他のリソースにアクセスできるアクセス許可がコードに付与されていても、このアクセス許可オブジェクトで指定されたリソースにしかアクセスできません。

RequestMinimum 8

コードを実行するのに必要な最小限のアクセス許可の要求。 この操作は、アセンブリのスコープ内でのみ使用できます。

RequestOptional 9

省略可能な追加のアクセス許可の要求 (実行には必要ありません)。 この要求は、個別に要求されていない、他のすべてのアクセス許可を暗黙的に拒否します。 この操作は、アセンブリのスコープ内でのみ使用できます。

RequestRefuse 10

誤用される可能性のあるアクセス許可が呼び出し元コードに付与されないようにする要求。 この操作は、アセンブリのスコープ内でのみ使用できます。

この例では、呼び出されたメソッドのコードに だけ IsolatedStoragePermissionがあることを CLR に通知する方法と、分離ストレージから書き込みと読み取りを行う方法も示します。

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.

注釈

注意事項

コード アクセス セキュリティ (CAS) は、.NET Framework と .NET のすべてのバージョンで非推奨になりました。 最近のバージョンの .NET では、CAS 関連の API が使われている場合、CAS の注釈は使われず、エラーが発生します。 開発者は、代わりの手段を見つけてセキュリティ タスクを実現する必要があります。

次の表では、各セキュリティ アクションが実行される時間と、それがサポートするターゲットについて説明します。

重要

.NET Framework 4 では、Deny、RequestMinimum、RequestOptional、および RequestRefuse アクセス許可要求を適用するためのランタイム サポートが削除されました。 これらの要求は、.NET Framework 4 以降に基づくコードでは使用しないでください。 この変更とその他の変更の詳細については、「 セキュリティの変更」を参照してください。

.NET Framework 4 では を使用LinkDemandしないでください。 代わりに、 を SecurityCriticalAttribute 使用して、完全に信頼されたアプリケーションへの使用を制限するか、 を使用 Demand して部分的に信頼された呼び出し元を制限します。

セキュリティ アクションの宣言 アクションの時刻 サポートされているターゲット
LinkDemand(.NET Framework 4 では使用しないでください) ジャストインタイム コンパイル クラス、メソッド
InheritanceDemand 読み込み時間 クラス、メソッド
Demand 実行時 クラス、メソッド
Assert 実行時 クラス、メソッド
Deny(.NET Framework 4 では廃止) 実行時 クラス、メソッド
PermitOnly 実行時 クラス、メソッド
RequestMinimum(.NET Framework 4 では廃止) 時間の付与 アセンブリ
RequestOptional(.NET Framework 4 では廃止) 時間の付与 アセンブリ
RequestRefuse(.NET Framework 4 では廃止) 時間の付与 アセンブリ

属性ターゲットの詳細については、「」を参照してください Attribute

適用対象