C#文件夹ACL未应用

时间:2015-06-19 02:34:26

标签: c# permissions directory acl

我最近开始学习C#工作项目,即编写更新的用户创建工具来替换旧的vbscript工具。到目前为止,我已经完成了它的所有Active Directory方面,但是在创建配置文件文件夹时我遇到了文件夹ACL的一些问题。

我已成功创建了一个删除所有文件夹ACL的功能,并从头开始,但我的功能是将ACL添加到该文件夹​​似乎不起作用。 这是功能:

public void CreateFolderACL(string FolderPath, string Account, FileSystemRights Rights, AccessControlType ControlType)
    {
        try
        {
            DirectorySecurity fs = Directory.GetAccessControl(FolderPath);
            AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
            fs.AddAccessRule(new FileSystemAccessRule(@"domain\" + Account, Rights, ControlType));
            Directory.SetAccessControl(FolderPath, fs);
        }
        catch(Exception E)
        {
            Console.WriteLine(E);
        }
    }

当我输入类似

的东西时

CreateFolderACL(userData["ProfilePath"] + ".v2", "Domain Admins", FileSystemRights.FullControl, AccessControlType.Allow);

它在文件夹中创建一个条目但没有设置权限(请参见下面的屏幕截图),并且它没有设置我尝试与域管理员一起应用的任何其他权限。

http://i.stack.imgur.com/Iul1i.png

我是新手,这是我的第一个真正的计划,但我遇到了障碍,无法弄清楚发生了什么。

具体错误是:System.Security.Principal.IdentityNotMappedException: Some or all identity references could not be translated.

2 个答案:

答案 0 :(得分:0)

听起来它不喜欢你的AddAccessRule。看看:Set File access rule 类似的东西

答案 1 :(得分:0)

感谢您的回复。

只是一个更新,我玩了一下,结果是"部分或全部身份参考无法翻译。"消息实际上是在尝试为刚创建的用户帐户添加权限时,它无法找到用户。

一旦我对其进行了评论并且仅测试了添加域管理员权限,它就会完成执行而不会出现错误,但会出现同样的问题。 Domain Admins是作为权限添加的,但仅限于#34;特殊权限"检查是否意味着完全控制不适用。

没有错误消息我无法弄清楚为什么会发生这种情况。我认为这可能是因为我在应用新ACL之前删除了所有ACL,因此我尝试创建一个文件夹并添加Domain Admins权限,而不先删除所有ACL,并且同样的事情发生。

如果我可以创建文件夹并从所述文件夹中清除所有ACL,那么它肯定不是权限问题吗?

如果我右键单击并尝试手动勾选Domain Admins的完全控制并点击apply,我会被拒绝访问。我不确定从哪里开始。

编辑:如果我不删除ACL,并且我将InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly添加到该行,则会很好地应用权限。一旦我首先尝试使用ACL删除,它就会以同样的方式失败。这必须是权限问题,或者我没有正确重建ACL。

最终编辑:我把它全部排序了。结果我需要添加两个访问规则:

fs.AddAccessRule(new FileSystemAccessRule(new System.Security.Principal.SecurityIdentifier(Account), Rights, ControlType));
            fs.AddAccessRule(new FileSystemAccessRule(new System.Security.Principal.SecurityIdentifier(Account), Rights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, ControlType));

感谢您的帮助。