检查以下两个代码块:
System.Security.AccessControl.DirectorySecurity dsec = System.IO.Directory.GetAccessControl(str);
System.Security.Principal.NTAccount group= new System.Security.Principal.NTAccount("DOMAIN","USERGROUP");
System.Security.AccessControl.FileSystemAccessRule myrule = new System.Security.AccessControl.FileSystemAccessRule(group,System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow);
dsec.SetAccessRule(myrule);
System.IO.Directory.SetAccessControl(str,dsec);
和
System.Security.AccessControl.FileSecurity fsec = System.IO.File.GetAccessControl(file);
System.Security.Principal.NTAccount group= new System.Security.Principal.NTAccount("DOMAIN","USERGROUP");
System.Security.AccessControl.FileSystemAccessRule myrule = new System.Security.AccessControl.FileSystemAccessRule(group,System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow);
fsec.SetAccessRule(myrule);
System.IO.File.SetAccessControl(file,fsec);
人们会期望它们都做完全相同的事情,只是一个目录和一个文件。而且,在某些方面,他们确实如此。在这两种情况下,所讨论的文件系统对象都会发生变化,使得 DOMAIN\USERGROUP 具有完全控制的有效权限。
但是,奇怪的是,当您右键单击一个文件并查看安全性时,您会看到:
当您右键单击一个文件夹并查看安全性时,您会看到:
如果我然后转到 Advanced->Effective Permissions->Select(DOMAIN\USERGROUP),它显示该文件夹的有效权限,对于该组,是完全控制(所有的框都被选中,而不仅仅是完全控制盒。那会更奇怪)。
我的问题是,为什么几乎相同的实现的效果会有所不同,有人知道如何复制对文件应用权限的效果吗?
最佳答案
区别在于传播标志与目录安全的相关性。
var accessRule = new FileSystemAccessRule(
identity: group,
fileSystemRights: FileSystemRights.FullControl,
type: AccessControlType.Allow,
inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
propagationFlags: PropagationFlags.None);
注意 inheritanceFlags
设置。如果未指定,则默认为无,被归类为“特殊”。
关于c# - DirectorySecurity 设置特殊权限,而 FileSecurity 没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11852057/