无法在新目录上检查 'Include inheritable permissions'

Unable to check 'Include inheritable permissions' on new directory

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

我试过使用继承和传播标志的替代值,以及 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);

不确定是否相关,但我正在使用它为用户创建主目录,所以计划是让用户具有修改权限并继承或添加本地系统和管理员 users/groups具有完全权限。

我已经在 Whosebug 上尝试 many other examples,但似乎没有检查那个讨厌的框。有什么想法吗?

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

此图显示第二个 window 中的灰色复选框和第三个 window 中的可访问复选框。在我的例子中,当我使用上面的代码创建文件夹时,两个框都未选中(并使用 SetAccessRuleProtection 或继承和传播标志的任何变体参数。

我的目的是在创建目录时选中那些 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);

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