12

我正在尝试以递归方式通过 Active Directory 获取用户的所有直接报告。因此,给定一个用户,我最终会得到一个列表,其中列出了所有将此人作为经理或将某人作为经理、将某人作为经理......最终将输入用户作为经理的所有用户的列表。

我目前的尝试相当缓慢:

private static Collection<string> GetDirectReportsInternal(string userDN, out long elapsedTime)
{
    Collection<string> result = new Collection<string>();
    Collection<string> reports = new Collection<string>();

    Stopwatch sw = new Stopwatch();
    sw.Start();

    long allSubElapsed = 0;
    string principalname = string.Empty;

    using (DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("LDAP://{0}",userDN)))
    {
        using (DirectorySearcher ds = new DirectorySearcher(directoryEntry))
        {
            ds.SearchScope = SearchScope.Subtree;
            ds.PropertiesToLoad.Clear();
            ds.PropertiesToLoad.Add("directReports");
            ds.PropertiesToLoad.Add("userPrincipalName");
            ds.PageSize = 10;
            ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
            SearchResult sr = ds.FindOne();
            if (sr != null)
            {
                principalname = (string)sr.Properties["userPrincipalName"][0];
                foreach (string s in sr.Properties["directReports"])
                {
                    reports.Add(s);
                }
            }
        }
    }

    if (!string.IsNullOrEmpty(principalname))
    {
        result.Add(principalname);
    }

    foreach (string s in reports)
    {
        long subElapsed = 0;
        Collection<string> subResult = GetDirectReportsInternal(s, out subElapsed);
        allSubElapsed += subElapsed;

        foreach (string s2 in subResult)
        {
        result.Add(s2);
        }
    }



    sw.Stop();
    elapsedTime = sw.ElapsedMilliseconds + allSubElapsed;
    return result;
}

本质上,这个函数将一个可分辨的名称作为输入(CN=Michael Stum, OU=test, DC=sub, DC=domain, DC=com),因此对 ds.FindOne() 的调用很慢。

我发现搜索 userPrincipalName 要快得多。我的问题:sr.Properties["directReports"] 只是一个字符串列表,这就是 distinctName,搜索起来似乎很慢。

我想知道,有没有一种快速的方法可以在 distinctName 和 userPrincipalName 之间进行转换?或者,如果我只有 distinctName 可以使用,是否有更快的方法来搜索用户?

编辑:感谢答案!搜索经理字段将功能从 90 秒提高到 4 秒。这是新的和改进的代码,它更快,更易读(请注意,elapsedTime 功能中很可能存在错误,但该函数的实际核心工作):

private static Collection<string> GetDirectReportsInternal(string ldapBase, string userDN, out long elapsedTime)
{
    Collection<string> result = new Collection<string>();

    Stopwatch sw = new Stopwatch();
    sw.Start();
    string principalname = string.Empty;

    using (DirectoryEntry directoryEntry = new DirectoryEntry(ldapBase))
    {
        using (DirectorySearcher ds = new DirectorySearcher(directoryEntry))
        {
            ds.SearchScope = SearchScope.Subtree;
            ds.PropertiesToLoad.Clear();
            ds.PropertiesToLoad.Add("userPrincipalName");
            ds.PropertiesToLoad.Add("distinguishedName");
            ds.PageSize = 10;
            ds.ServerPageTimeLimit = TimeSpan.FromSeconds(2);
            ds.Filter = string.Format("(&(objectCategory=user)(manager={0}))",userDN);

            using (SearchResultCollection src = ds.FindAll())
            {
                Collection<string> tmp = null;
                long subElapsed = 0;
                foreach (SearchResult sr in src)
                {
                    result.Add((string)sr.Properties["userPrincipalName"][0]);
                    tmp = GetDirectReportsInternal(ldapBase, (string)sr.Properties["distinguishedName"][0], out subElapsed);
                    foreach (string s in tmp)
                    {
                    result.Add(s);
                    }
                }
            }
          }
        }
    sw.Stop();
    elapsedTime = sw.ElapsedMilliseconds;
    return result;
}
4

1 回答 1

10

首先,当您已经拥有要查找的 DN 时,无需将 Scope 设置为“子树”。

另外,如何找到所有“manager”属性是您要查找的人的对象,然后对其进行迭代。这通常应该比其他方式更快。

(&(objectCategory=user)(manager=<user-dn-here>))

编辑:以下内容很重要,但到目前为止仅在对此答案的评论中提到:

如上所述构建过滤器字符串时,存在使用对 DN 有效但在过滤器中具有特殊含义的字符破坏它的风险。这些必须被转义

*   as  \2a
(   as  \28
)   as  \29
\   as  \5c
NUL as  \00
/   as  \2f

// Arbitrary binary data can be represented using the same scheme.

编辑:将 设置SearchRoot为对象的 DN,并且也是SearchScopeBase单个对象拉出 AD 的快速方法。

于 2008-10-10T08:51:38.000 回答