c# - 无法检查新目录上的 'Include inheritable permissions'

标签 c# .net windows inheritance

我正在尝试创建新目录并以编程方式设置权限。我已经能够创建目录,为单个用户和组应用权限,并更新大多数继承和传播设置。但是,我无法使用代码检查 Window GUI 界面中的 Include inheritable permissions from this object's parent 选项(见下文)。

我试过使用 Inheritance 和 Propogation 标志的替代值,以及 SetAccessRuleProtection 参数,但我似乎找不到任何有效的排列。

代码示例:

using System.Security.AccessControl;

//...

string dir = @"C:\MyTestFolder";

DirectorySecurity dirSec = new DirectorySecurity();

dirSec.AddAccessRule(
    new FileSystemAccessRule(@"contoso.com\myuser",
        FileSystemRights.FullControl,
        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
        PropagationFlags.None,
        AccessControlType.Allow));

dirSec.SetAccessRuleProtection(false, true);

Directory.CreateDirectory(dir, dirSec);

不确定它是否相关,但我正在使用它为用户创建主目录,因此计划是让用户具有修改权限并继承或添加具有完全权限的 SYSTEM 和 Administrators 本地用户/组。

我试过了 many other examples从 StackOverflow 上的这里开始,但似乎没有任何东西可以检查那个讨厌的框。有什么想法吗?

编辑:这里是我所指的复选框。请注意,这些不是我的特定屏幕截图,而只是演示。

This image 此图像显示第二个窗口中的灰色复选框,以及第三个窗口中的可访问复选框。就我而言,当我使用上述代码创建文件夹时(并使用 SetAccessRuleProtection 或 Inheritance 和 Propagation 标志的参数的任何变体),两个框都未选中。

我的意图是在创建目录时选中这些 GUI 复选框(并因此在后台设置它们的适当权限),以便它看起来与图像类似。

最佳答案

看起来这一切都归结为操作顺序。似乎如果我通过提供 DirectorySecurity 创建目录,它将创建文件夹,文件夹权限被我提供的内容完全覆盖。而且,出于某种原因,如果我调用 SetAccessRuleProtection 来为我在创建过程中 提供的 DirectorySecurity 设置文件夹的可继承权限,它不会发生。

但是,如果我先创建文件夹而不提供任何权限信息,则创建时默认选中该框,然后我可以根据需要添加我的 AccessRule:

using System.Security.AccessControl;

//...

string dir = @"C:\MyTestFolder";

//If you update and add the access rules here, it doesn't work

Directory.CreateDirectory(dir);

//Now that the folder has been created...
DirectorySecurity dirSec = Directory.GetAccessControl(dir);
dirSec.AddAccessRule(
    new FileSystemAccessRule(@"contoso.com\myuser",
        FileSystemRights.FullControl,
        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
        PropagationFlags.None,
        AccessControlType.Allow));
//this isn't really necessary at this point, but wouldn't hurt:
//dirSec.SetAccessRuleProtection(false, true);
Directory.SetAccessControl(dir, dirSec);

我不知道为什么必须按此特定顺序执行此操作的原因,但如果有人有洞察力,我很想听听原因。无论如何,这让我现在可以毫无问题地工作。

关于c# - 无法检查新目录上的 'Include inheritable permissions',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28174414/

相关文章:

ASP.Net 错误 : “The type ‘foo’ exists in both ”temp1. dll“和”temp2.​​dll“(第 2 部分)

C# 8 从具体类型中调用默认实现

windows - 在 Windows 上设置共享的远程 git 存储库的最快和最简单的方法是什么?

c# - 服务需要检测工作站是否被锁定,屏幕保护程序是否处于事件状态

c# - 请求的操作导致堆栈溢出

c# - 将收件人电子邮件地址添加到电子邮件正文

javascript - Windows 8 固定网站磁贴

c# - .Net 中有关 Google Drive 的错误

c# - Struct 隐式默认构造函数与无参数构造函数

c - DLL 注入(inject)器不工作(64 位编译器)