다음을 통해 공유


방법: RequestRefuse 플래그를 사용하여 권한 거부

업데이트: 2007년 11월

코드가 시스템 리소스를 악의적인 의도로 액세스할 가능성이 있는 경우 해당 코드에 특정 권한이 부여되지 않도록 요청할 수 있습니다. 예를 들어, 파일 내의 데이터를 찾아보지만 수정하지는 않는 응용 프로그램은 파일 쓰기 권한을 거부할 수 있습니다. 버그나 악의적인 공격이 발생하는 경우에도 이 코드는 사용하는 데이터를 손상시킬 수 없습니다.

RequestRefuse를 사용하면 구체적인 특정 권한은 확실히 부여하지 않으면서 대규모의 권한 집합을 선택적 권한으로 요청할 수 있습니다.

다음 예제에서는 RequestRefuse를 사용하여 공용 언어 런타임 보안 시스템에서 FileIOPermission을 거부합니다.

예제

Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Permissions
'The request is placed at the assembly level. 
<assembly: FileIOPermission(SecurityAction.RequestRefuse, Unrestricted := True)>

Namespace MyNameSpace
   Public Class MyClass1
      Public Sub New()
      End Sub
      
      Public Shared Sub Main()
         'Creation of the log is attempted in the try block.
         Try
            Dim TextStream As New StreamWriter("Log.txt")
            TextStream.WriteLine("This Log was created on {0}", DateTime.Now)
            TextStream.Close()
            Console.WriteLine("The Log was 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.RequestRefuse ,Unrestricted = true)]

namespace MyNameSpace
{
   using System;
   using System.Security;
   using System.Security.Permissions;
   using System.IO;

   public class MyClass
   {
      public MyClass()
     {       
     }
      
      public static int Main(string[] args)
      {
         //Creation of the log is attempted in the try block.
         try
         {   
            StreamWriter TextStream = new StreamWriter("Log.txt");
            TextStream.WriteLine("This Log was created on {0}", DateTime.Now);
            TextStream.Close();
            Console.WriteLine("The Log was 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.");
        }
      

         return 0;
      }
   }
}    

앞의 예제에는 파일을 생성할 수 있는 권한이 부여되지 않으므로 보안 예외가 생성됩니다. catch 문은 예외를 가로채며 응용 프로그램은 다음 메시지를 콘솔에 표시합니다.

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

참고 항목

개념

권한 요청

기타 리소스

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

코드 액세스 보안