【问题标题】:Why can I not set this ACL rule in C#?为什么我不能在 C# 中设置此 ACL 规则?
【发布时间】:2010-10-05 03:52:20
【问题描述】:

在 Vista SP1 上以提升的管理员身份运行,我的 C# 应用程序尝试使用以下代码设置以下规则。不会产生错误,但目录的 ACL 也不会发生任何更改。我错过了什么?

public static void Main( string args[] )
{
    string dirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Company"), "Product" );
    Directory.Create(dirPath);
    _SetAcl(dirPath, "Users", FileSystemRights.FullControl);
}

private static void _SetAcl(string path, string identity, FileSystemRights rights)
{
    var info = new DirectoryInfo(path);
    var acl = info.GetAccessControl();

    var rule1 = new FileSystemAccessRule(identity, rights, AccessControlType.Allow);
    bool modified;
    acl.ModifyAccessRule(AccessControlModification.Reset, rule1, out modified);

    var inheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
    var rule2 = new FileSystemAccessRule(identity, rights, inheritanceFlags,
                                        PropagationFlags.InheritOnly, AccessControlType.Allow);
    acl.ModifyAccessRule(AccessControlModification.Add, rule2, out modified);
}

更新:只需将以下代码添加为_SetAcl方法的最后一行,我的代码就可以了。

info.SetAccessControl(acl);

【问题讨论】:

    标签: c# .net security acl rights


    【解决方案1】:

    要完成该过程,您必须使用修改后的 ACL 调用 DirectoryInfo.SetAccessControl()。

    GetAccessControl() 真正返回 ACL 的副本。您可以随意修改它,但在您调用 SetAccessControl() 之前它不会生效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多