【问题标题】:C# Using SearchResultCollection to enumerate large group of AD usersC# 使用 SearchResultCollection 枚举大量 AD 用户
【发布时间】:2012-09-05 08:37:47
【问题描述】:

所以我现在在一个特定的 AD 组中有超过 1500 个用户,当我将他们拉下来时,我会限制我得到的用户。我在 MSDN (http://msdn.microsoft.com/en-us/library/ms180907%28v=vs.80%29.aspx) 上看到了这篇文章,但是它执行FindOne(),这样做会使应用程序超过 10分钟拉下用户。使用ResultsCollection,我可以在 30 秒内打开应用程序。

什么时候去处理

string Last_Name = userResults.Properties["sn"][0].ToString();

返回错误:

索引超出范围。必须是非负数且小于 集合。\r\n参数名称:索引"}

我认为这存在找不到结果的问题,但是,ResultsCollection 包含所有 1000 个条目。任何帮助表示赞赏。谢谢!

注意:这些用户的姓氏不能为空,问题是resultCollection 只返回 1 个属性,即adpath

DirectoryEntry dEntryhighlevel = new DirectoryEntry("LDAP://OU=Clients,OU=x,DC=h,DC=nt");

DirectorySearcher dSeacher = new DirectorySearcher(dEntryhighlevel);
dSeacher.Filter = "(&(objectClass=user)(memberof=CN=Users,,OU=Clients,OU=x,DC=h,DC=nt))";

uint rangeStep = 1000;
uint rangeLow = 1;
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);
    }

    dSeacher.PropertiesToLoad.Clear();
    dSeacher.PropertiesToLoad.Add(attributeWithRange);

    SearchResultCollection resultCollection = dSeacher.FindAll();

    foreach (SearchResult userResults in resultCollection)
    {
        string Last_Name = userResults.Properties["sn"][0].ToString();
        string First_Name = userResults.Properties["givenname"][0].ToString();
        string userName = userResults.Properties["samAccountName"][0].ToString();
        string Email_Address = userResults.Properties["mail"][0].ToString();
        OriginalList.Add(Last_Name + "|" + First_Name + "|" + userName + "|" + Email_Address);

        if (userResults.Properties.Contains(attributeWithRange))
        {
            foreach (object obj in userResults.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);

【问题讨论】:

  • 如果姓氏为空,很可能返回null。这就是为什么你不能要求它的第一个字母 (["sn"][0])。这会给你空引用。
  • @WiktorZychla 这些用户的姓氏不为空,问题是 resultCollection 只返回 1 个属性,那就是“adpath”

标签: c# active-directory


【解决方案1】:

看来,如果您添加一个 PropertiesToLoad,它将不再加载任何其他属性。因此,就我而言,您必须指定要加载的所有属性。

dSeacher.PropertiesToLoad.Clear();
                dSeacher.PropertiesToLoad.Add(attributeWithRange);
                dSeacher.PropertiesToLoad.Add("givenname");
                dSeacher.PropertiesToLoad.Add("sn");
                dSeacher.PropertiesToLoad.Add("samAccountName");
                dSeacher.PropertiesToLoad.Add("mail");

【讨论】:

    【解决方案2】:

    我不会走这条路。通过对组的基本搜索来读取成员资格比像这样进行大型子树搜索更容易(并且更不容易出错)。

    正如您所观察到的,当您尝试读取大型组的成员属性时,每次读取只会获得 1500 个值。让所有成员离开组的方法是通过一种通常称为“远程检索”的功能。 我在这里提供了相关信息的链接:Always getting 1500 member of distribution list using PowerShell

    【讨论】:

    • 无论如何,我解释了如果您在其他线程中继续使用这种方法,如何处理这个问题。我希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2017-09-09
    相关资源
    最近更新 更多