如何查看是否为文件或文件夹取消选中"包含可继承权限"?

Jer*_*emy 7 .net c# windows filesystems permissions

我在C#中编写了一个小实用程序,以确保指定的文件夹及其所有内容都具有适当的访问权限(我希望为该Authenticated Users组提供完全访问权限).以下代码似乎可以正常用于更新顶级文件夹的ACL(访问控制列表):

SecurityIdentifier allUsers = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
FileSystemAccessRule newRule = new FileSystemAccessRule(allUsers,
    FileSystemRights.FullControl, iFlags,
    PropagationFlags.None, AccessControlType.Allow);

DirectoryInfo info = new DirectoryInfo(folderPath);
DirectorySecurity security = info.GetAccessControl();
security.AddAccessRule(newRule);
info.SetAccessControl(security);
Run Code Online (Sandbox Code Playgroud)

但是,我注意到,此新访问规则不会传播到其安全属性中未选中"包含可继承权限..."选项的子文件夹.这才有意义.所以,我想要做的是为任何这样的子文件夹重新打开安全权限继承.

我挖掘的ObjectSecurity.SetAccessRuleProtection方法应该是我需要的一半.但是,对于已经继承父级DACL的对象盲目使用上述方法似乎很草率.因此,我想确定哪些对象的权限继承被关闭,但我似乎无法找到返回此信息的相应方法或属性.有吗?我在这里错过了什么吗?

Mel*_*Mel 5

我记得使用过这样的东西:

DirectoryInfo d = new DirectoryInfo(@"e:\test1");
DirectorySecurity acl = d.GetAccessControl();
if (acl.GetAccessRules(false, true, typeof(System.Security.Principal.SecurityIdentifier)).Count >0)
    // -- has inherited permissions
else
    // -- has no inherited permissions
Run Code Online (Sandbox Code Playgroud)

我也试图找到一种方法来检查这个,但我找不到任何(即使在 C++ 中)。所以我最终使用了上面的代码。它就像一个魅力。

  • 您的示例仅显示“如果继承了某些东西”,而“如果继承已打开,则不显示”。当父文件夹仅将权限传播到父文件夹(而不是子文件夹或文件)或 acl 损坏时,这是一个区别。在这两种情况下,您都不会获得“即使打开时也不会继承”。 (2认同)

Xtr*_*ity 3

C# 的DirectorySecurity类现在似乎包含当继承为和时AreAccessRulesProtected返回的属性。truedisabledfalseenabled

因此,您可以简单地使用:

DirectorySecurity dirSecurity = Directory.GetAccessControl(pathToDir);
var isInheritanceEnabled = !dirSecurity.AreAccessRulesProtected

Run Code Online (Sandbox Code Playgroud)

感谢@Wizou在这里的评论提醒!