Export (0) Print
Expand All

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

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

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.

Community Additions

ADD
Show:
© 2014 Microsoft