Permission Requests


Permission Requests

An assembly can declaratively specify certain permission requests. For example, it can specify a minimal set of permissions that it absolutely requires. If the CLR determines that the required permissions are not to be granted, then the assembly will not even load. The assembly can also specify a nice-to-have set of permissions that, if not granted, will still allow the assembly to load. Assemblies can also specify permissions that are flat-out refused , avoiding the risk of having any dangerous permissions that are simply not needed by the assembly.

The PermissionRequest Example

The PermissionRequest example shows how to make permission requests within an assembly. Three attributes are established using the UIPermission attribute, which governs the ability to use the clipboard and windowing features of the user interface. The first attribute specifies RequestMinimum for all clipboard operations, RequestOptional for unrestricted user interface operations, and RequestRefuse for all window operations.

The effect of the first permission request is that when the program is run, if the permission to access the clipboard is not granted according to security policy, the assembly load operation aborts with a security exception. There is not much effect in this example from the second permission request, but it essentially specifies that all user interface operations be considered optional. The third permission request states that we would like to proactively prevent any possibility of any type of window usage. When you run the program, the TryWindowAccess method attempts to display a message box, which fails with a security exception.

 //PermissionRequest.cs using System; using System.Security; using System.Security.Permissions; using System.Windows.Forms; [assembly:UIPermission(    SecurityAction.  RequestMinimum  ,    Clipboard=UIPermissionClipboard.AllClipboard)] [assembly:UIPermission(    SecurityAction.  RequestOptional  ,    Unrestricted=true)] [assembly:UIPermission(    SecurityAction.  RequestRefuse  ,    Window=UIPermissionWindow.AllWindows)] public class PermissionRequest {    public static void Main()    {       Console.WriteLine("Calling TryClipboardAccess");       TryClipboardAccess();       Console.WriteLine("Calling TryWindowAccess");       TryWindowAccess();       Console.Write("Press any key");       Console.Read();    }    private static void TryClipboardAccess()    {       try       {  Clipboard.SetDataObject  (             "TryClipboardAccess", true);       }       catch (SecurityException se)       {          Console.WriteLine(se.Message);       }    }    private static void TryWindowAccess()    {       try       {  MessageBox.Show  ("TryWindowAccess");       }       catch (SecurityException se)       {          Console.WriteLine(se.Message);       }    } } 


.NET Security and Cryptography
.NET Security and Cryptography
ISBN: 013100851X
EAN: 2147483647
Year: 2003
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net