Search code examples
c#directorypermissionsactive-directoryfile-permissions

Apply this "folder only" permissions for AD Groups to a folder


I'm struggling a bit with folder permissions.

I want to basically add a AD group to a folder with modify access but then restrict it.

Problem is I couldn't figure out how to apply a permission to "this folder only"

The goal is to set the following restrictions to a main folder:

  • deny delete subfolders
  • deny delete
  • deny change permissions
  • deny take ownership

Here is a picture of the windows GUI

I found the AccessRule Class but I can't find any detail on how to do this with C#

Does anyone know how to do this?


Solution

  • I found a solution. Here is the code and the info:

    //set params for all access sets
    AccessControlType DenyAccess = AccessControlType.Deny;
    AccessControlType AllowAccess = AccessControlType.Allow;
    InheritanceFlags inheritFlag = InheritanceFlags.None;
    InheritanceFlags inheritFlag2 = InheritanceFlags.ContainerInherit;
    InheritanceFlags inheritFlag3 = InheritanceFlags.ObjectInherit;
    PropagationFlags propagationFlags = PropagationFlags.None;
    FileSystemRights access = FileSystemRights.ChangePermissions;
    FileSystemRights access2 = FileSystemRights.Delete;
    FileSystemRights access3 = FileSystemRights.TakeOwnership;
    FileSystemRights access4 = FileSystemRights.DeleteSubdirectoriesAndFiles;
    FileSystemRights ReadAccess = FileSystemRights.ReadAndExecute;
    FileSystemRights ModifyAccess = FileSystemRights.Modify;
    
    DirectoryInfo info = new DirectoryInfo(strPath);
    DirectorySecurity security = info.GetAccessControl();
    
    //set read right for group
    NTAccount GroupRead = new NTAccount(StrDomain, strGroupRead);
    security.AddAccessRule(new FileSystemAccessRule(GroupRead, ReadAccess, inheritFlag2, propagationFlags, AllowAccess));
    security.AddAccessRule(new FileSystemAccessRule(GroupRead, ReadAccess, inheritFlag3, propagationFlags, AllowAccess));
    
    //set Modify right for group
    NTAccount GroupModify = new NTAccount(StrDomain, strGoupModify);
    security.AddAccessRule(new FileSystemAccessRule(GroupModify, ModifyAccess, inheritFlag2, propagationFlags, AllowAccess));
    security.AddAccessRule(new FileSystemAccessRule(GroupModify, ModifyAccess, inheritFlag3, propagationFlags, AllowAccess));
    
    //set special right group
    security.AddAccessRule(new FileSystemAccessRule(groupModify, access, inheritFlag, propagationFlags, DenyAccess)); //ChangePermission
    security.AddAccessRule(new FileSystemAccessRule(groupModify, access2, inheritFlag, propagationFlags, DenyAccess)); //Delete
    security.AddAccessRule(new FileSystemAccessRule(groupModify, access3, inheritFlag, propagationFlags, DenyAccess)); //Ownership
    security.AddAccessRule(new FileSystemAccessRule(groupModify, access4, inheritFlag, propagationFlags, DenyAccess)); //Delete subfiles and folders
    
    //add rights to folder
    info.SetAccessControl(security);
    

    This gives you a folder with a read and modify group and the modify group can't delete the main folder and also the members can't take ownership over it nor change the permissions on it.

    cheers