c# - DirectorySecurity 设置特殊权限,而 FileSecurity 没有

标签 c# .net permissions implementation

检查以下两个代码块:

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 具有完全控制的有效权限。

但是,奇怪的是,当您右键单击一个文件并查看安全性时,您会看到: File Security Tab

当您右键单击一个文件夹并查看安全性时,您会看到: Folder Security Tab

如果我然后转到 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/

相关文章:

c# - DataSourceAttribute 的工作原理

asp.net - .NET WebAPI 在本地主机上运行良好,但在 Azure 上不起作用

linux - Docker 组成卷权限 linux

c# - 引用并存储动态创建的控件中的数据?

c# - 使用 CakeBuild 构建 Xamarin.Android 项目时找不到 mscorlib

c# - 在 Entity Framework 中使用存储过程(代码优先)

mysql - 将主机访问权限重新分配给 MySQL 用户

SVN 文件夹(非存储库范围)权限

c# - 类型 'Microsoft.SqlServer.Types.SqlGeography' 存在于 'Microsoft.SqlServer.Types.dll' 和 'Microsoft.SqlServer.Types.dll' 中

c# - 为什么 NumberFormatInfo 中包含货币而不是其他单位