英語で読む

次の方法で共有


SecurityAction 列挙型

定義

注意事項

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

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

C#
[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
C#
public enum SecurityAction
C#
[System.Serializable]
public enum SecurityAction
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
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 に通知する方法と、分離ストレージから書き込みと読み取りを行う方法も示します。

C#
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.

注釈

注意事項

コード アクセス セキュリティ (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

適用対象

製品 バージョン (廃止)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1 (5, 6, 7, 8, 9)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
Windows Desktop (5, 6, 7, 8, 9)