This topic has not yet been rated - Rate this topic

Requesting XML-Encoded Permissions

Important noteImportant

In the .NET Framework version 4, runtime support has been removed for the RequestMinimum, RequestOptional, and RequestRefuse permission requests. This topic does not apply to code that is based on the .NET Framework 4 or later. For more information about this and other changes, see Security Changes in the .NET Framework 4.

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

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.