【问题标题】:get SamAccountName from large AD groups with DirectorySearcher使用 DirectorySearcher 从大型 AD 组中获取 SamAccountName
【发布时间】:2015-07-10 05:12:36
【问题描述】:

我正在使用以下方法从大型 AD 组中查询成员。

try
{
    DirectoryEntry entry = new DirectoryEntry("LDAP://CN=My Distribution List,OU=Distribution Lists,DC=Fabrikam,DC=com");
    DirectorySearcher searcher = new DirectorySearcher(entry);
    searcher.Filter = "(objectClass=*)";

    uint rangeStep = 1000;
    uint rangeLow = 0;
    uint rangeHigh = rangeLow + (rangeStep - 1);
    bool lastQuery = false;
    bool quitLoop = false;

    do
    {
        string attributeWithRange;
        if(!lastQuery)
        {
            attributeWithRange = String.Format("member;range={0}-{1}", rangeLow, rangeHigh);
        }
        else
        {
            attributeWithRange = String.Format("member;range={0}-*", rangeLow);
        }           
        searcher.PropertiesToLoad.Clear();
        searcher.PropertiesToLoad.Add(attributeWithRange);
        SearchResult results = searcher.FindOne();
        foreach(string res in results.Properties.PropertyNames)
        {
            System.Diagnostics.Debug.WriteLine(res.ToString());
        }
        if(results.Properties.Contains(attributeWithRange))
        {
            foreach(object obj in results.Properties[attributeWithRange])
            {
                Console.WriteLine(obj.GetType());
                if(obj.GetType().Equals(typeof(System.String)))
                {
                }
                else if (obj.GetType().Equals(typeof(System.Int32)))
                {
                }
                Console.WriteLine(obj.ToString());
            }
            if(lastQuery)
            {
                quitLoop = true;
            }
        }
        else
        {
            lastQuery = true;
        }
        if(!lastQuery)
        {
            rangeLow = rangeHigh + 1;
            rangeHigh = rangeLow + (rangeStep - 1);
        }
    }
    while(!quitLoop);
}
catch(Exception ex)
{
    // Handle exception ex.
}

在这里,我想检索成员的 SamAccountName 以及他们的 distinctName。此外,我还有一些包含跨域成员的组。你能帮忙吗?

【问题讨论】:

  • 任何帮助将不胜感激
  • 你能发布你的最终版本吗?答案有帮助吗?

标签: c# asp.net c#-4.0 active-directory active-directory-group


【解决方案1】:

要获取任何属性的值,您通常需要调用 DirectoryEntry.Properties[PropertyName].Value,例如

var x = results.Properties["distinguishedName"].Value

对于某些索引值,可能需要提供索引 Properties[PropertyName][X].Value

如果默认情况下未在结果中填充值,您可能需要使用 DirectorySearcher.PropertiesToLoad.Add(PropertyName);

强制它加载

【讨论】:

    猜你喜欢
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多