方法 : RequestOptional フラグを使用してオプションのアクセス許可を要求する

更新 : 2007 年 11 月

SecurityAction.RequestOptional フラグを使用すると、アクセス許可セットを要求できます。本来であればランタイムによって付与される、その他のすべてのアクセス許可は拒否します。これとは反対に、RequestRefuse フラグを使用すると、コードに付与されないことが必要なアクセス許可を明示的に拒否できます。

RequestMinimum フラグとは異なり、RequestOptional フラグを使用して要求したすべてのアクセス許可を受け取らない場合でもアプリケーションは実行され、プロテクトされたリソースにアクセスしようとすると SecurityException がスローされます。このような要求を使用する場合は、要求したオプションのアクセス許可が与えられなかった場合にスローされる例外を、そのコードがキャッチできるようにしておく必要があります。

SecurityAction.RequestOptional フラグを使用して、その他の許可をすべて間接的に拒否しながら FileIOPermission を要求する例を次に示します。この例では、Log という架空のクラスが LogNameSpace 内に存在することを前提としています。Log クラスには、ローカル コンピュータ上に新しいログ ファイルを作成する MakeLog メソッドが含まれています。このアプリケーションは、Log クラスの新しいインスタンスを作成し、try ブロックで MakeLog メソッドを実行します。また、catch キーワードを使用して、スローされたすべての SecurityException を受け取り、メッセージを表示します。

使用例

Imports System
Imports System.Security
'The hypothetical class log is in this namespace.
Imports LogNameSpace
Imports System.Security.Permissions
'The request is placed at the assembly level.
<assembly: FileIOPermission(SecurityAction.RequestOptional, Unrestricted := True)>

Namespace MyNamespace
   Public Class MyClass1
      
      Public Sub New()

      End Sub
      
      'Entry point that delegates to C-style main Private Function.
      Public Overloads Shared Sub Main()
         Main(System.Environment.GetCommandLineArgs())
      End Sub
      
      Overloads Public Shared Sub Main(args() As String)
         'Put any code that requires optional permissions in the try block. 
         Try
            Dim MyLog As New Log()
            MyLog.MakeLog()
            Console.WriteLine("The Log has been created.")
         'Catch the security exception and inform the user that the 
         'application was not granted FileIOPermission.
         Catch
            Console.WriteLine("This application does not have permission to write to the disk.")
         End Try
      End Sub
   End Class
End Namespace     
//The request is placed at the assembly level.
using System.Security.Permissions;
[assembly:FileIOPermission(SecurityAction.RequestOptional, Unrestricted = true)]

namespace MyNamespace {
   using System;
   using System.Security;
   //The hypothetical class log is in this namespace.
   using LogNameSpace;

   public class MyClass {
      public MyClass() {
      }

      public static void Main(string[] args) {
         //Put any code that requires optional permissions in the try block. 
         try {
            Log MyLog = new Log();
            MyLog.MakeLog();
            Console.WriteLine("The Log has been created.");
         }
         //Catch the security exception and inform the user that the 
         //application was not granted FileIOPermission.
         catch(SecurityException) {
            Console.WriteLine("This application does not have permission to write to the disk.");
         }
      }
   }
}

上に示したコードは、必要な許可が与えられた場合は、ログ ファイルを作成し、コンソールに次のメッセージを表示します。

The Log has been created.

このコードが共有から実行され、そのようなコードには FileIOPermission を与えないようにローカルのセキュリティが設定されている場合、コードは十分なアクセス許可を受け取ることができず、次のメッセージを表示します。

This application does not have permission to write to the disk.

参照

概念

アクセス許可の要求

参照

SecurityAction

FileIOPermission

UIPermission

その他の技術情報

属性を使用したメタデータの拡張

コード アクセス セキュリティ