using System.Security.AccessControl; using System.IO; using System.Security.Principal;
Get Directory access control and audit security (ACL)
DirectorySecurity Fileacl = Directory.getaccesscontrol (FilePath);
Folder Permissions Inheritance Control
Fileacl.setaccessruleprotection (truetrue); // permission inheritance is removed, and inherited access rules are preserved fileacl.setaccessruleprotection (truetrue); // the permission inheritance is removed and the inherited access Rule fileacl.setaccessruleprotection (falsetrue); // permission inheritance is allowed, and the second argument is ignored
Access Rule access control entry creation
FileSystemAccessRule everyonerule = new FileSystemAccessRule (" everyone " , Filesystemrights.fullcontrol, Accesscontroltype.allow); FileSystemAccessRule everyoneRule1 = new FileSystemAccessRule ( " everyone " , Filesystemrights.modify, Accesscontroltype.allow);
//"Everyone" user account name
//filesystemrights.fullcontrol access rules
//accesscontroltype.allow allows access to protected objects, Accesscontroltype.deny denies access to protected objects
The SID, also known as the security Identifiers, is the unique number that identifies the user, group, and computer accounts. When you create the account for the first time,
The internal process in the # will refer to the account's SID instead of the account's user or group name.
If you create an account, delete the account, and then create another account with the same user name, the new account will not have the rights or permissions to authorize the previous account.
The reason is that the account has a different SID number. Security identifiers are also known as security IDs or SIDS.
modifying access control lists (ACLs)
BOOLresult =false; //adds the specified authorization rule to the Access Control List (ACL). Fileacl.modifyaccessrule (Accesscontrolmodification.add, Everyonerule, outresult); //removes the authorization rule with the same security identifier (SID) and access mask as the specified authorization rule from the ACL. Fileacl.modifyaccessrule (Accesscontrolmodification.remove, Everyonerule, outresult); //removes the authorization rule with the same SID as the specified authorization rule from the ACL. Fileacl.modifyaccessrule (Accesscontrolmodification.removeall, Everyonerule, outresult); //removes an authorization rule that exactly matches the specified authorization rule from the ACL. Fileacl.modifyaccessrule (accesscontrolmodification.removespecific, Everyonerule, outresult); //Removes the authorization rule with the same SID as the specified authorization rule from the ACL, and then adds the specified authorization rule to the ACL. Fileacl.modifyaccessrule (Accesscontrolmodification.reset, Everyonerule, outresult); //removes all authorization rules from the ACL, and then adds the specified authorization rule to the ACL. Fileacl.modifyaccessrule (Accesscontrolmodification.set, Everyonerule, outresult); //permission AppendFileacl.addaccessrule (Everyonerule); //permission DeleteFileacl.removeaccessrule (Everyonerule); //removes all matching allow or Deny access control List (ACL) permissions from the current file or directory. Fileacl.removeaccessruleall (Everyonerule); //removes all Access control List (ACL) permissions for the specified user from the current file or directory. fileacl.removeaccessrulespecific (Everyonerule); //removes a single matching allow or Deny access control List (ACL) permission from the current file or directory.
Iterating through the Aces in the ACL
Authorizationrulecollection rules = Fileacl.getaccessrules (truetruetypeof( NTAccount)); // authorizationrule Rule = rules[0]; foreach inch rules) {
if (Rule.IdentityReference.Value.CompareTo ("Users") ==0)
{ fileacl.removeaccessrule (rule);
} }
C # folder permissions operations collation