1

我们最近被迫迁移到世界另一端的新域服务器。这可能看起来没有太大变化,但我们经常运行的进程之一突然从 2 秒命令变为 5 分钟命令。

原因?我们正在基于“模板”目录结构更新许多目录的权限。

我们发现 XCOPY 可以在相同的两秒窗口中更新这些设置中的大部分。当然,剩下的设置就不用了。

我想弄清楚的是,XCopy 如何能更快地完成 .NET 安全类本机应该做的事情?显然我错过了一些东西。

在不 ping(或最少 ping)域/Active Directory 服务器的情况下复制目录的 ACL 信息的最佳方法是什么?

这是我所拥有的:

    ... 

    DirectorySecurity TemplateSecurity = new DirectorySecurity(TemplateDir, AccessControlSections.All);

    ... 


    public static void UpdateSecurity(string destination, DirectorySecurity TemplateSecurity)
    {
        DirectorySecurity dSecurity = Directory.GetAccessControl(destination);

        // Remove previous security settings. (We have all we need in the other TemplateSecurity setting!!)
        AuthorizationRuleCollection acl_old = dSecurity.GetAccessRules(true, true, typeof(NTAccount));
        foreach (FileSystemAccessRule ace in acl_old)
        {
            // REMOVE IT IF YOU CAN... if you can't don't worry about it.
            try
            {
                dSecurity.RemoveAccessRule(ace);
            }
            catch { }
        }

        // Remove the inheritance for the folders... 
        // Per the business unit, we must specify permissions per folder.
        dSecurity.SetAccessRuleProtection(true, true);

        // Copy the permissions from TemplateSecurity to Destination folder.
        AuthorizationRuleCollection acl = TemplateSecurity.GetAccessRules(true, true, typeof(NTAccount));

        foreach (FileSystemAccessRule ace in acl)
        {
            // Set the existing security.
            dSecurity.AddAccessRule(ace);

            try
            {
                // Remove folder inheritance...
                dSecurity.AddAccessRule(new FileSystemAccessRule(
                    ace.IdentityReference, ace.FileSystemRights,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    ace.AccessControlType));
            }
            catch { }
        }

        // Apply the new changes.
        Directory.SetAccessControl(destination, dSecurity);
    }

4

1 回答 1

2

好的...在互联网上进行大量挖掘之后,我有一个工作原型。不用说,网上有很多关于 ACL 的错误信息。我不确定这些信息是天赐之物,还是更多错误信息。我将不得不把它留给你,用户,来决定。

我最终得到的是干净、流畅、非常非常快的,因为它永远不会接触到域服务器。我直接复制 SDDL 条目。等等,你说......你不能在目录上这样做,因为你得到可怕的 SeSecurityPrivilege 错误!

如果您将副本限制为仅访问控制列表 (ACL),则不会。

这是代码:

    public static void UpdateSecurity(string destination, DirectorySecurity templateSecurity)
    {
        DirectorySecurity dSecurity = Directory.GetAccessControl(destination);

        string sddl = templateSecurity.GetSecurityDescriptorSddlForm(AccessControlSections.Access);
        try
        {
            // TOTALLY REPLACE The existing access rights with the new ones.
            dSecurity.SetSecurityDescriptorSddlForm(sddl, AccessControlSections.Access);

            // Disable inheritance for this directory.
            dSecurity.SetAccessRuleProtection(true, true);


            // Apply these changes.
            Directory.SetAccessControl(destination, dSecurity);
        }
        catch (Exception ex)
        {
            // Note the error on the console... we can formally log it later.
            Console.WriteLine(pth1 + " : " + ex.Message);
        }


        // Do some other settings stuff here...

    }

请注意AccessControlSections.AccessSDDL 方法上的标志。那是使这一切正常工作的神奇钥匙。

于 2009-09-22T19:19:12.407 回答