15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

564 ❘ ChaPTer 21 security<br />

➤<br />

➤<br />

Internet specifi es the default policy for code of unknown origin. This is the most restrictive policy<br />

listed. For example, code executing in this permission set has no fi le IO capability, cannot read or<br />

write event logs, <strong>and</strong> cannot read or write environment variables.<br />

Everything grants all the permissions that are listed under this set, except the permission to skip code<br />

verifi cation. The administrator can alter any of the permissions in this permission set. This is useful<br />

when the default policy needs to be tighter.<br />

Note that you can change the defi nitions of only the Everything permission<br />

set — the other sets are fi xed <strong>and</strong> cannot be changed. Of course, you can also create<br />

your own permission set.<br />

Dem<strong>and</strong>ing Permissions Programmatically<br />

An assembly can dem<strong>and</strong> permissions declaratively or programmatically. The following code snippet<br />

demonstrates how permissions can be dem<strong>and</strong>ed with the method Dem<strong>and</strong>FileIOPermissions() . If you<br />

import the namespace System.Security.Permissions , you can check for permissions by creating a<br />

FileIOPermission object, <strong>and</strong> calling its Dem<strong>and</strong>() method. This verifi es if the caller of the method, here<br />

the caller of the method Dem<strong>and</strong>FileIOPermissions , has the required permissions. In case the Dem<strong>and</strong>()<br />

method fails, an exception of type SecurityException is thrown. It ’ s okay not to catch the exception <strong>and</strong><br />

let it be h<strong>and</strong>led by the caller.<br />

using System;<br />

using System.Security;<br />

using System.Security.Permissions;<br />

[assembly: AllowPartiallyTrustedCallers()]<br />

namespace Wrox.ProCSharp.Security<br />

{<br />

[SecuritySafeCritical]<br />

public class Dem<strong>and</strong>Permissions<br />

{<br />

public void Dem<strong>and</strong>FileIOPermissions(string path)<br />

{<br />

var fileIOPermission = new FileIOPermission(PermissionState.Unrestricted);<br />

fileIOPermission.Dem<strong>and</strong>();<br />

}<br />

}<br />

}<br />

//...<br />

code snippet Dem<strong>and</strong>PermissionDemo/Dem<strong>and</strong>Permissions.cs<br />

FileIOPermission is contained within the System.Security.Permissions namespace, which is home to<br />

the full set of permissions <strong>and</strong> also provides classes for declarative permission attributes <strong>and</strong> enumerations<br />

for the parameters that are used to create permissions objects (for example, creating a FileIOPermission<br />

specifying whether read - only or full access is needed).<br />

To catch exceptions thrown by the CLR when code attempts to act contrary to its granted permissions,<br />

you can catch the exception of the type SecurityException , which provides access to a number of useful<br />

pieces of information, including a human - readable stack trace ( SecurityException.StackTrace ) <strong>and</strong> a<br />

reference to the method that threw the exception ( SecurityException.TargetSite ). SecurityException<br />

even provides you with the SecurityException.PermissionType property, which returns the type of<br />

Permission object that caused the security exception to occur.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!