This post contains one example which shows how to deal with the Access Control Lists by using of .NET 2.0.
It enlists all access rules from the specified folder. Additionally, this example shows how to remove
the access control rule of the specified account (in this case NT-Account "Everyone").
Last but not least, the example shows how to set a new access rule for the account which has been removed.
static void Main(string[] args) { string dir = @"C:\Temp\ACLTEST"; AuthorizationRuleCollection rules;
DirectorySecurity dirSec = Directory.GetAccessControl(dir);
// Gets the list of all access-rules of the specified folder. rules = dirSec.GetAccessRules(true, true, typeof(NTAccount));
// Enumerates all access rules set on the specified folder. foreach (FileSystemAccessRule rule in rules) {
Console.WriteLine("Identity: {4}\nType: {0}\nRights {1}\nPropagation Flags: {2}\nInherited: {3}", rule.AccessControlType, rule.FileSystemRights, rule.PropagationFlags, rule.IsInherited, rule.IdentityReference);
Console.WriteLine("........................."); }
// // Removes access rules for account 'Everyone' if such // rules exists. foreach (FileSystemAccessRule rule in rules) { if (rule.IdentityReference.Value == "Everyone") dirSec.RemoveAccessRule(rule); }
// Create the identity reference for account 'Everyone'. NTAccount everyOne = new NTAccount("everyone");
// Creates the new rule for account 'Everyone'. // Permissions are propagated to Folders, Subfolders and items. FileSystemAccessRule sbbNetRule = new FileSystemAccessRule(everyOne, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);
dirSec.AddAccessRule(sbbNetRule); DirectoryInfo dInfo = new DirectoryInfo(dir); dInfo.SetAccessControl(dirSec); } |
The list bellow shows what flags have to be set by creating of FileSystemAccessRule to establish wanted scenario.
Subfolders and Files only:
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit
PropagationFlags.InheritOnly
This Folder, Subfolders and Files:
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit
PropagationFlags.None
This Folder, Subfolders and Files:
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.NoPropagateInherit
This folder and subfolders:
InheritanceFlags.ContainerInherit,
PropagationFlags.None
Subfolders only:
InheritanceFlags.ContainerInherit,
PropagationFlags.InheritOnly
This folder and files:
InheritanceFlags.ObjectInherit,
PropagationFlags.None
This folder and files:
InheritanceFlags.ObjectInherit,
PropagationFlags.NoPropagateInherit
Posted
Jun 18 2007, 12:23 PM
by
Damir Dobric