Udostępnij za pośrednictwem


How to: Request Permission for a Named Permission Set

Important noteImportant

In the .NET Framework version 4, runtime support has been removed for enforcing the Deny, RequestMinimum, RequestOptional, and RequestRefuse permission requests. Do not use these requests in code that is based on .NET Framework 4 or later. For more information about this and other changes, see Security Changes in the .NET Framework 4.

Instead of requesting individual permissions (using RequestMinimum, RequestOptional, or RequestRefuse), you can request any of the following built-in permission sets: Nothing, Execution, FullTrust, Internet, LocalIntranet, and SkipVerification. You cannot request custom named permission sets or the Everything modifiable built-in permission set because the permissions they represent can vary. The following example shows the syntax to request permission for a named permission set. It attaches a PermissionSetAttribute with a Name value representing the name of the desired permission set.

Example

Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
'The attribute is placed at the assembly level.
<assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name := "FullTrust")>
Namespace MyNamespace
   Public Class [MyClass]
      Public Sub New()
      End Sub
      
      Public Sub MyMethod()
         'Perform operations that require permissions here.
      End Sub 
   End Class
End Namespace
//The attribute is placed at the assembly level.
using System.Security.Permissions;
[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")]
namespace MyNamespace
{
   using System;
   using System.Runtime.InteropServices;
   
   public class MyClass
   {
      public MyClass()
      {
      }
      public void MyMethod()
      {
      //Perform operations that require permissions here.
      }
   }
}

See Also

Concepts

Requesting Permissions

Other Resources

Extending Metadata Using Attributes

Code Access Security