1

I encounter a problem to assign access rights to "Everyone" on a directory folder "wwwroot" on windows from my little programm in C#. Here's how I do it.

//I also try with 'S-1-1-0'/'Everyone' but it's the same result 
string userPermission = "Everyone"  ;

DirectoryInfo myDirRoot = new DirectoryInfo(myArmsUpdate.InstallationPath);
DirectorySecurity myDirectorySecurity = myDirRoot.GetAccessControl();
FileSystemAccessRule myPermission = new FileSystemAccessRule(userPermission , FileSystemRights.ReadAndExecute, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);

myDirectorySecurity.AddAccessRule(myPermission);
myDirRoot.SetAccessControl(myDirectorySecurity);

However I still get the same error:

System.Security.Principal.IdentityNotMappedException

Mehdi Bugnard
  • 3,889
  • 4
  • 45
  • 86

1 Answers1

2

Try the following,

DirectorySecurity sec = Directory.GetAccessControl(path);
        // Using this instead of the "Everyone" string means we work on non-English systems.
        SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
        sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
        Directory.SetAccessControl(path, sec);

Note: You must be an administrator to make this work

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • It's perfect ! Thanks . Now all work fine – Mehdi Bugnard Feb 08 '13 at 10:03
  • That code looks suspiciously like what is [found here](http://stackoverflow.com/a/5398398/109702). If you can copy code like this then the question should be closed as a duplicate. – slugster Nov 19 '14 at 02:49