C#中使用SearchResultCollection枚举大群AD用户大群、用户、SearchResultCollection、AD

2023-09-09 21:40:20 作者:一生醉笑

所以,我刚才怎么回事是我有超过1500个用户在一个特定的AD组,而当我拉下来,我只能按我是谁得到的。我看到这篇文章的MSDN(http://msdn.microsoft.com/en-us/library/ms180907%28v=vs.80%29.aspx)。但是它执行 FindOne(),这样做需要的应用程序超过10分钟,拉下用户。随着 ResultsCollection 我能够在30秒内打开的应用程序。

当它去处理。

 字符串姓氏= userResults.Properties [SN] [0]的ToString();
 

它回来与错误:

  

索引超出范围。必须是非负并小于大小   收集\ r \ n参数名:索引} 的

我想这有没有找到结果但是, ResultsCollection 包含了所有1000个条目的问题。任何帮助是AP preciated。谢谢!

作为一个说明:姓氏不为空为这些用户,这个问题是 resultCollection 仅返回一间酒店,这就是 adpath

 的DirectoryEntry dEntryhighlevel =新的DirectoryEntry(LDAP:// OU =客户,OU = X,DC = H,DC = NT);

DirectorySearcher从dSeacher =新DirectorySearcher从(dEntryhighlevel);
dSeacher.Filter =(及(对象类=用户)(的memberOf = CN =用户​​,, OU =客户,OU = X,DC = H,DC = NT));

UINT rangeStep = 1000;
UINT rangeLow = 1;
UINT rangeHigh = rangeLow +(rangeStep -1);
布尔lastQuery = FALSE;
布尔quitLoop = FALSE;

做
{
    串attributeWithRange;

    如果(!lastQuery)
    {
        attributeWithRange =的String.Format(成员;范围= {0}  -  {1},rangeLow,rangeHigh);
    }
    其他
    {
        attributeWithRange =的String.Format(成员;范围= {0}  -  *,rangeLow);
    }

    dSeacher.PropertiesToLoad.C​​lear();
    dSeacher.PropertiesToLoad.Add(attributeWithRange);

    SearchResultCollection resultCollection = dSeacher.FindAll();

    的foreach(在resultCollection信息搜索结果userResults)
    {
        字符串姓氏= userResults.Properties [SN] [0]的ToString();
        将First_Name串= userResults.Properties [给定名称] [0]的ToString();
        字符串username = userResults.Properties [的samAccountName] [0]的ToString();
        字符串EMAIL_ADDRESS = userResults.Properties [电子邮件] [0]的ToString();
        OriginalList.Add(姓氏+|将First_Name + +|+的userName +|+ EMAIL_ADDRESS);

        如果(userResults.Properties.Contains(attributeWithRange))
        {
            的foreach(在userResults.Properties obj对象[attributeWithRange])
            {
                Console.WriteLine(obj.GetType());

                如果(obj.GetType()。等于(typeof运算(System.String)))
                {
                }
                否则,如果(obj.GetType()。等于(typeof运算(System.Int32的)))
                {
                }

                Console.WriteLine(obj.ToString());
            }

            如果(lastQuery)
            {
                quitLoop = TRUE;
            }
        }
        其他
        {
           lastQuery = TRUE;
        }

        如果(!lastQuery)
        {
            rangeLow = rangeHigh + 1;
            rangeHigh = rangeLow +(rangeStep  -  1);
        }
   }
}
而(quitLoop!);
 
枚举如何设置空白 Simulink Tutorial 2 枚举 宏定义 强制类型转换及类型别名

解决方案

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

  dSeacher.PropertiesToLoad.C​​lear();
                dSeacher.PropertiesToLoad.Add(attributeWithRange);
                dSeacher.PropertiesToLoad.Add(给定名称);
                dSeacher.PropertiesToLoad.Add(SN);
                dSeacher.PropertiesToLoad.Add(的samAccountName);
                dSeacher.PropertiesToLoad.Add(邮件);
 

So what I have going on is I have over 1500 users in a specific AD group, and when I pull them down I'm limited to as who I get. I saw this article on MSDN (http://msdn.microsoft.com/en-us/library/ms180907%28v=vs.80%29.aspx) however it performs a FindOne(), doing this takes the app over 10 minutes to pull down the users. With ResultsCollection I'm able to open the app in 30 seconds.

When it goes to process

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

it comes back with the error:

Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}

I figured this had the problem of not finding the results however, the ResultsCollection contains all 1000 entries. Any help is appreciated. Thanks!

As a note: The surname is not empty for these users, the issue is resultCollection is only returning 1 property and that's 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);

解决方案

It appears that if you add one PropertiesToLoad that it will no longer load any other properties. As such you have to specify all properties you want to load, in my case.

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

 
精彩推荐
图片推荐