
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
992
|
Chapter 17: Security
The code shown in Example 17-18 grants and then revokes the ability to delete a file.
Discussion
When granting or revoking access rights on a file or registry key, you need two
things. The first is a valid
NTAccount object. This object essentially encapsulates a
user or group account. A valid
NTAccount object is required in order to create either a
new
RegistryAccessRule or a new FileSystemAccessRule. The NTAccount identifies the
user or group this access rule will apply to. Note that the string passed in to the
Example 17-18. Granting and revoking the right to delete a file
public static void GrantRevokeFileRights( )
{
NTAccount user = new NTAccount(@"WRKSTN\ST");
string file = @"c:\FOO.TXT";
GrantFileRights(file, user, FileSystemRights.Delete, InheritanceFlags.None,
PropagationFlags.None, AccessControlType.Allow);
RevokeFileRights(file, user, FileSystemRights.Delete, InheritanceFlags.None,
PropagationFlags.None, AccessControlType.Allow);
}
public static void GrantFileRights(string file,
NTAccount user,
FileSystemRights rightsFlags,
InheritanceFlags inherFlags,
PropagationFlags propFlags,
AccessControlType actFlags)
{
FileSecurity fileSecurity = File.GetAccessControl(file);
FileSystemAccessRule rule = new FileSystemAccessRule(user, rightsFlags,
inherFlags, propFlags, ...