Export (0) Print
Expand All

How to: Request Optional Permissions by Using the RequestOptional Flag 

The SecurityAction.RequestOptional flag allows you to request a set of permissions while refusing all other permissions the runtime otherwise might have been willing to give. By contrast, the RequestRefuse flag allows you to refuse permissions by explicitly specifying which ones your code should not be granted.

In contrast to using the RequestMinimum flag, your application will execute if it does not receive all the permissions that you request using the RequestOptional flag, and a SecurityException will be thrown when your application attempts to access a protected resource. If you use this type of request, you must enable your code to catch any exceptions that will be thrown if your code is not granted the optional permission.

The following example requests FileIOPermission using the SecurityAction.RequestOptional flag, indirectly refusing all other permissions. This example assumes that a hypothetical class Log exists in LogNameSpace. The Log class contains the MakeLog method that creates a new log file on the local computer. This application creates a new instance of the Log class and executes the MakeLog method in the try block. Using the catch keyword, it intercepts any SecurityException thrown and displays a message.

Example

No code example is currently available or this language may not be supported.

The previous code creates the log file and displays the following message to the console if it has sufficient permissions:

The Log has been created.

If the code is run from a share and the local security settings do not allow such code to have FileIOPermission, the code is not granted sufficient permission and displays the following message:

This application does not have permission to write to the disk.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft