Requesting XML-Encoded Permissions

If you need to request a custom permission set (using RequestMinimum, RequestOptional, or RequestRefuse), you can use an XML representation of the desired permission set in one of two ways: either you can pass a string that contains the actual XML-encoded permission set or you can provide the location of an XML file containing the encoded permission set. The following example uses XML with the PermissionSetAttribute. The XML flag is a string containing an XML-encoded permission set, which in this case represents an unrestricted UIPermission and an unrestricted RegistryPermission.

'The attribute is placed at the assembly level.
Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
<assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, XML := "<PermissionSet class=""System.Security.PermissionSet"" version=""1""><Permission class=""System.Security.Permissions.UIPermission, mscorlib"" version=""1""><AllWindows/></Permission><Permission class=""System.Security.Permissions.RegistryPermission, mscorlib"" version=""1""><Unrestricted/></Permission></PermissionSet>")>
Namespace MyNamespace
   Public Class MyClass1
      Public Sub New()
      End Sub

      Public Sub MyMethod()
         'Perform user interface operations here.
      End Sub
   End Class
End Namespace
[C#]
//The attribute is placed at the assembly level.
using System.Security.Permissions;
[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum, XML="<PermissionSet class=\"System.Security.PermissionSet\" version=\"1\"><Permission class=\"System.Security.Permissions.UIPermission, mscorlib\" version=\"1\"><AllWindows/></Permission><Permission class=\"System.Security.Permissions.RegistryPermission, mscorlib\" version=\"1\"><Unrestricted/></Permission></PermissionSet>")]
namespace MyNamespace 
{
   using System;
   using System.Runtime.InteropServices;
   public class MyClass 
   {
      public MyClass() 
      {
      }
      public void MyMethod() 
      {
        //Perform user interface operations here.
      }
   }
}

The following example shows a request for a custom permission set by providing the location and name of the file that contains the same XML-encoded permission set. If you do not specify the location of your XML file, the runtime looks for it in the same directory as the application.

Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
'The attribute is placed at the assembly level.
<assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, File := "pset.xml")>

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

Creating an XML-Encoded Permission Set

You can create an XML encoding of a permission set by creating an instance of the PermissionSet object, adding instances of the permissions you want to the object, and then calling the ToXml method to return a SecurityElement object that represents the XML encoding or calling the ToString method to return a string representation of the XML encoding.

See Also

Requesting Permissions | PermissionSetAttribute Class | PermissionSet Class | Metadata and Self-Describing Components | Extending Metadata Using Attributes | Code Access Security