15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

file security ❘ 805<br />

adding <strong>and</strong> removing aCls from a file<br />

It is also possible to manipulate the ACLs of a resource using the same objects that were used in the previous<br />

examples. The following code example changes a previous code example where a file’s ACL information was<br />

read. Here, the ACLs are read for a specified file, changed, <strong>and</strong> then read again:<br />

try<br />

{<br />

using (FileStream myFile = new FileStream(myFilePath,<br />

FileMode.Open, FileAccess.ReadWrite))<br />

{<br />

FileSecurity fileSec = myFile.GetAccessControl();<br />

Console.WriteLine("ACL list before modification:");<br />

foreach (FileSystemAccessRule fileRule in<br />

fileSec.GetAccessRules(true, true,<br />

typeof(System.Security.Principal.NTAccount)))<br />

{<br />

Console.WriteLine("{0} {1} {2} access for {3}", myFilePath,<br />

fileRule.AccessControlType == AccessControlType.Allow <br />

"provides": "denies",<br />

fileRule.FileSystemRights,<br />

fileRule.IdentityReference);<br />

}<br />

Console.WriteLine();<br />

Console.WriteLine("ACL list after modification:");<br />

FileSystemAccessRule newRule = new FileSystemAccessRule(<br />

new System.Security.Principal.NTAccount(@"PUSHKIN\Tuija"),<br />

FileSystemRights.FullControl,<br />

AccessControlType.Allow);<br />

fileSec.AddAccessRule(newRule);<br />

File.SetAccessControl(myFilePath, fileSec);<br />

}<br />

}<br />

foreach (FileSystemAccessRule fileRule in<br />

fileSec.GetAccessRules(true, true,<br />

typeof(System.Security.Principal.NTAccount)))<br />

{<br />

Console.WriteLine("{0} {1} {2} access for {3}", myFilePath,<br />

fileRule.AccessControlType == AccessControlType.Allow <br />

"provides": "denies",<br />

fileRule.FileSystemRights,<br />

fileRule.IdentityReference);<br />

}<br />

In this case, a new access rule is added to the file’s ACL. This is done by using the FileSystemAccessRule<br />

object. The FileSystemAccessRule class is an abstraction access control entry (ACE) instance. The ACE<br />

defines the user account to use, the type of access that this user account can deal with, <strong>and</strong> whether or<br />

not to allow or deny this access. In creating a new instance of this object, a new NTAccount is created <strong>and</strong><br />

given Full Control to the file. Even though a new NTAccount is created, it must still reference an existing<br />

user. Then the AddAccessRule method of the FileSecurity class is used to assign the new rule. From<br />

there, the FileSecurity object reference is used to set the access control to the file in question using the<br />

SetAccessControl() method of the File class.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!