Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

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.

Example

//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.

//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

Community Additions

ADD
Show:
© 2015 Microsoft