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.

Code access security ❘ 565<br />

If you just use the .<strong>NET</strong> classes for file I/O you don’t have to dem<strong>and</strong> the FileIOPermission yourself as<br />

this is dem<strong>and</strong>ed by the .<strong>NET</strong> classes doing file I/O. However, you need to make the dem<strong>and</strong> yourself if you<br />

wrap native API calls such as CreateFileTransacted(). Also, you can use this mechanism to dem<strong>and</strong><br />

custom permissions from the caller.<br />

Using the s<strong>and</strong>box aPi to Host Unprivileged Code<br />

By default, with a desktop application, the application has full trust. Using the S<strong>and</strong>box API, you can create<br />

an app-domain that doesn’t have full trust.<br />

To see the S<strong>and</strong>box API in action, first create a <strong>C#</strong> library project named RequireFileIOPermissionsDemo. This<br />

library contains the class RequirePermissionsDemo with the method RequireFilePermissions(). This<br />

method returns true or false, depending on whether the code has file permissions. With the implementation<br />

of this code, the File class creates a file where the path is passed with the argument variable path. In<br />

case writing the file fails, an exception of type SecurityException is thrown. The File class checks for<br />

the FileIOSecurity as you saw earlier with the Dem<strong>and</strong>PermissonDemo sample. If the security check<br />

fails, a SecurityException is thrown by the Dem<strong>and</strong>() method of the FileIOSecurity class. Here, the<br />

SecurityException is caught to return false from the RequireFilePermissions() method.<br />

using System;<br />

using System.IO;<br />

using System.Security;<br />

[assembly: AllowPartiallyTrustedCallers()]<br />

namespace Wrox.ProCSharp.Security<br />

{<br />

[SecuritySafeCritical]<br />

public class RequirePermissionsDemo : MarshalByRefObject<br />

{<br />

public bool RequireFilePermissions(string path)<br />

{<br />

bool accessAllowed = true;<br />

try<br />

{<br />

StreamWriter writer = File.CreateText(path);<br />

writer.WriteLine("written successfully");<br />

writer.Close();<br />

}<br />

catch (SecurityException)<br />

{<br />

accessAllowed = false;<br />

}<br />

}<br />

}<br />

}<br />

return accessAllowed;<br />

code snippet RequireFileIOPermissionsDemo/RequirePermissionsDemo.cs<br />

The hosting application where the S<strong>and</strong>box API is used is the project AppDomainHost that is a simple<br />

<strong>C#</strong> console application. The S<strong>and</strong>box API is an overload of the AppDomain.CreateDomain() method that<br />

creates a new app-domain in a s<strong>and</strong>box. This method requires four parameters including the name of the<br />

app-domain, the evidence that is taken from the current app-domain, the AppDomainSetup information,<br />

<strong>and</strong> a permission set. The permission set that is created only contains SecurityPermission with the flag<br />

SecurityPermissionFlag.Execution so that the code is allowed to execute — nothing more. In the<br />

new s<strong>and</strong>boxed app-domain, the object of type Dem<strong>and</strong>Permissions in the assembly Dem<strong>and</strong>Permission<br />

is instantiated.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!