如何将访问权限“Everyone”分配给目录

时间:2013-02-08 09:40:22

标签: c# .net security permissions windows-identity

我在C#中的小程序窗口上的目录文件夹“ wwwroot ”上分配了对“所有人”的访问权限时遇到问题。 我就是这样做的。

//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);

但是我仍然得到同样的错误:

  

System.Security.Principal.IdentityNotMappedException

1 个答案:

答案 0 :(得分:2)

尝试以下方法,

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);

注意:您必须是管理员才能开展此工作