2

我正在尝试使用代码从 Active Directory 中获取所有用户:

PrincipalContext ad = new PrincipalContext(contextType, adserviceName, adContext, ContextOptions.SimpleBind, username, password);
UserPrincipal u = new UserPrincipal(ad) {Name = "*"};
PrincipalSearcher search = new PrincipalSearcher { QueryFilter = u };
foreach (var principal in search.FindAll()) 
{
    //do something 
}

但它只返回前 1000 行。如何在不使用 DirectorySearcher 的情况下检索所有用户。谢谢。

4

3 回答 3

2

我认为如果不使用DirectorySearcher ,您将无法做到这一点。

代码片段 -

// set the PageSize on the underlying DirectorySearcher to get all entries
((DirectorySearcher)search.GetUnderlyingSearcher()).PageSize = 1000;

另请参阅如果 OU 包含 3000 个用户,如何使用 DirectorySearcher 查找所有用户?

于 2015-11-20T11:24:29.967 回答
1

您需要获取底层证券DirectorySearcher并在其上设置PageSize属性:

using (PrincipalContext ad = new PrincipalContext(contextType, adserviceName, adContext, ContextOptions.SimpleBind, username, password))
{ 
    UserPrincipal u = new UserPrincipal(ad) {Name = "*"};

    PrincipalSearcher search = new PrincipalSearcher { QueryFilter = u };

    // get the underlying "DirectorySearcher"
    DirectorySearcher ds = search.GetUnderlyingSearcher() as DirectorySearcher;

    if(ds != null)
    {
        // set the PageSize, enabling paged searches
       ds.PageSize = 500;
    }


    foreach (var principal in search.FindAll()) 
    {
        //do something 
    }
}
于 2015-11-20T11:30:57.717 回答
1

你可以:

((DirectorySearcher)myPrincipalSearcher.GetUnderlyingSearcher()).SizeLimit = 20;
于 2019-01-07T16:38:17.943 回答