How to grant permissions for folders in C #?

I need to give the "Temporary ASP.NET Files" folder write permission using C # ... and I use this code to give it access

DirectoryInfo d1 = new DirectoryInfo(Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "Temporary ASP.NET Files")); DirectorySecurity md1 = d1.GetAccessControl(); string user_1 = fa.TextGuestDomain + "\\" + fa.TextGuestUser; md1.AddAccessRule(new FileSystemAccessRule(user_1, FileSystemRights.FullControl,InheritanceFlags.ObjectInherit,PropagationFlags.InheritOnly, AccessControlType.Allow)); d1.SetAccessControl(md1); 

When I checked the security properties for the ASP.NET Temporary Files folder after implementing the code, he did not check the “write permission” box, instead he checked the “special permissions” one ... I noticed that even when I changed the access from write full control or read it, he checked the "special permissions" one ....

This is not a problem :), the problem is that he does not give the correct access, which I give him ... when I let him write, it does not work if I give him permission to write. I do not know why! Am I doing it wrong?

Note: when I do this in manual mode of my work, and when using the encoding method. he does not work...

I hope you help me with this ...

thanks a lot

+6
source share
2 answers

I know that your pain - the ACL for the file system - is a pain that needs to be changed, and even if it works, it can break under some circumstances. Fortunately, in your case there is a simple solution.

The problem is PropagationFlags.InheritOnly . This means that this permission applies only to elements that inherit permissions - for example. you grant rights only to files in this directory, and not in any subdirectories.

To grant rights to directories that inherit "normally" (that is, apply to subdirectories and all files), use the following values ​​for InheritanceFlags and PropagationFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit and PropagationFlags.None .

+7
source
  private static void GrantAccess(string file) { bool exists = System.IO.Directory.Exists(file); if (!exists) { DirectoryInfo di = System.IO.Directory.CreateDirectory(file); Console.WriteLine("The Folder is created Sucessfully"); } else { Console.WriteLine("The Folder already exists"); } DirectoryInfo dInfo = new DirectoryInfo(file); DirectorySecurity dSecurity = dInfo.GetAccessControl(); dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); dInfo.SetAccessControl(dSecurity); } 

The above code sets the access rights to the folder for full control / read-write to each user (each).

+1
source

Source: https://habr.com/ru/post/888805/


All Articles