다음을 통해 공유


방법: RequestMinimum 플래그를 사용하여 최소 권한 요청

업데이트: 2007년 11월

RequestMinimum 플래그를 사용하면 코드를 실행하는 데 필요한 최소 권한 집합을 요청할 수 있습니다. 반면, RequestRefuse 플래그를 사용하면 코드에 부여하지 않을 권한을 명시적으로 지정하여 권한을 거부할 수 있습니다.

RequestMinimum 플래그를 사용할 때와는 달리, RequestOptional 플래그를 사용하여 요청한 모든 권한을 받지 않아도 응용 프로그램이 실행되며 응용 프로그램에서 보호되는 리소스에 액세스하려고 하면 SecurityException이 throw됩니다. 이러한 요청 형식을 사용하는 경우 코드에 선택적 권한이 부여되지 않을 때 throw되는 예외를 코드에서 catch할 수 있어야 합니다.

다음 예제에서는 RequestMinimum 플래그를 사용하여 FileIOPermission을 요청합니다. 이 예제는 요청된 권한이 부여되지 않은 경우 실행되지 않습니다. 이 예제에서는 LogNameSpace에 가상 클래스인 Log가 있다고 가정합니다. Log 클래스에는 로컬 컴퓨터에 새 로그 파일을 만드는 MakeLog 메서드가 포함되어 있습니다. 이 응용 프로그램은 Log 클래스의 새 인스턴스를 만들고 try 블록에 있는 MakeLog 메서드를 실행합니다. 이 응용 프로그램은 catch 키워드를 사용하여, throw된 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

기타 리소스

특성을 사용하여 메타데이터 확장

코드 액세스 보안