繁体   English   中英

C#使用SearchResultCollection枚举大批AD用户

[英]C# Using SearchResultCollection to enumerate large group of AD users

因此,我要做的是,在一个特定的AD组中有1500多个用户,当我将其拉低时,我的身份将仅限于获得谁。 我在MSDN(http://msdn.microsoft.com/zh-cn/library/ms180907%28v=vs.80%29.aspx)上看到了这篇文章,但是它执行了FindOne() ,这样做使应用程序超过了10个分钟拉下用户。 使用ResultsCollection我可以在30秒内打开该应用程序。

何时进行处理

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

它返回错误:

索引超出范围。 必须为非负数且小于集合的大小。\\ r \\ n参数名称:index“}

我发现这有一个找不到结果的问题,但是, 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);

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

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

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

正如您所观察到的,当您尝试在一个大型组上读取member属性时,每次读取将仅获得1500个值。 使小组成员脱身的方法是通过通常称为“远程检索”的功能。 我在此处提供了有关此信息的链接: 使用PowerShell始终获得1500个分发列表的成员

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM