方法 : RequestMinimum フラグを使用して最小のアクセス許可を要求する

更新 : 2007 年 11 月

RequestMinimum フラグを使用すると、コードを実行するために必要な最小のアクセス許可セットを要求できます。これとは反対に、RequestRefuse フラグを使用すると、コードに付与しないアクセス許可を明示的に指定することによりこれらのアクセス許可を拒否できます。

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

RequestMinimum フラグを使用して 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.RequestMinimum, 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.RequestMinimum, 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

その他の技術情報

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

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