C # folder permissions operations collation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.