我可以从 PrincipalSearcher 获得超过 1000 条记录吗?

Can I get more than 1000 records from a PrincipalSearcher?

我正在尝试使用代码从 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 
}

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

我认为如果不使用 DirectorySearcher.

,您将无法做到这一点

代码片段-

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

另见 If an OU contains 3000 users, how to use DirectorySearcher to find all of them?

您需要获取基础 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 
    }
}

您可以:

((DirectorySearcher)myPrincipalSearcher.GetUnderlyingSearcher()).SizeLimit = 20;