我可以从PrincipalSearcher获得1000多条记录吗

本文关键字:记录 1000 PrincipalSearcher 获得 我可以 | 更新日期: 2023-09-27 18:22:46

我正在尝试使用代码从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的情况下检索所有用户。谢谢

我可以从PrincipalSearcher获得1000多条记录吗

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

代码片段-

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

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

您需要获取底层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;