如果一个OU包含3000个用户,如何使用DirectorySearcher找到所有用户?

发布于 2024-11-24 16:44:52 字数 574 浏览 2 评论 0 原文

我使用这段代码:

DirectoryEntry objEntry;
DirectorySearcher objSearchEntry;
SearchResultCollection objSearchResult;
string strFilter = "(&(objectCategory=User))";
objEntry = new DirectoryEntry(conOUPath, conUser, conPwd, AuthenticationTypes.Secure);
objEntry.RefreshCache();
objSearchEntry = new DirectorySearcher(objEntry);
objSearchEntry.Filter=strFilter;
objSearchEntry.SearchScope=SearchScope.Subtree;
objSearchEntry.CacheResults=false;
objSearchResult=objSearchEntry.FindAll();

每次只返回 1000 个用户,但该 OU 中有 3000 个用户。

我怎样才能找到所有这些?

I use this code:

DirectoryEntry objEntry;
DirectorySearcher objSearchEntry;
SearchResultCollection objSearchResult;
string strFilter = "(&(objectCategory=User))";
objEntry = new DirectoryEntry(conOUPath, conUser, conPwd, AuthenticationTypes.Secure);
objEntry.RefreshCache();
objSearchEntry = new DirectorySearcher(objEntry);
objSearchEntry.Filter=strFilter;
objSearchEntry.SearchScope=SearchScope.Subtree;
objSearchEntry.CacheResults=false;
objSearchResult=objSearchEntry.FindAll();

Each time, it only return 1000 users, but there are 3000 users in that OU.

How can i find all of them ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水波映月 2024-12-01 16:44:52

如果您使用的是 .NET 3.5 或更高版本,您应该查看 PrincipalSearcher 和“按示例查询”主体来进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=SomeOU,DC=YourCompany,DC=com");

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) of "Bruce"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// set the PageSize on the underlying DirectorySearcher to get all 3000 entries
((DirectorySearcher)srch.GetUnderlyingSearcher()).PageSize = 500;

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

如果您还没有 - 绝对要阅读 MSDN文章 在 .NET Framework 中管理目录安全主体3.5 很好地展示了如何充分利用 System.DirectoryServices.AccountManagement 中的新功能

更新:

当然,根据您的需要,您可以想要在您创建的“按示例查询”用户主体上指定其他属性:

  • Surname(或姓氏)
  • DisplayName(通常:名字 + 空格 + 姓氏)
  • SAM 帐户名称 - 您的 Windows/AD 帐户名称
  • 用户主体名称 - 您的“[email protected]" 样式名称

您可以指定 UserPrincipal 上的任何属性,并将它们用作您的“按示例查询” PrincipalSearcher

更新#2:如果您想仅在给定 OU 内部进行搜索,则可以在 PrincipalContext 的构造函数中定义该 OU。

If you're on .NET 3.5 or newer, you should check out the PrincipalSearcher and a "query-by-example" principal to do your searching:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=SomeOU,DC=YourCompany,DC=com");

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) of "Bruce"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// set the PageSize on the underlying DirectorySearcher to get all 3000 entries
((DirectorySearcher)srch.GetUnderlyingSearcher()).PageSize = 500;

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement

Update:

Of course, depending on your need, you might want to specify other properties on that "query-by-example" user principal you create:

  • Surname (or last name)
  • DisplayName (typically: first name + space + last name)
  • SAM Account Name - your Windows/AD account name
  • User Principal Name - your "[email protected]" style name

You can specify any of the properties on the UserPrincipal and use those as "query-by-example" for your PrincipalSearcher.

Update #2: If you want to search just inside a given OU, you can define that OU in the constructor of the PrincipalContext.

吾家有女初长成 2024-12-01 16:44:52

您需要设置 DirectorySearcher.PageSize 属性能够返回所有结果。例如:

objSearchEntry.PageSize = 500;

否则返回的项目数量将受到服务器端的限制,默认为1000。还有一个叫做 SizeLimit 的东西,如果你想明确限制返回的项目数量,你可以设置它。如果 SizeLimit 和 PageSize 均为 0(默认值),则它将使用服务器端默认 SizeLimit。我认为有点违反直觉。

如果要返回所有结果,唯一的方法是将PageSize设置为非零值,将SizeLimit设置为0。

You need to set the DirectorySearcher.PageSize property to be able to return all the results. For example:

objSearchEntry.PageSize = 500;

Otherwise the number of items returned will be limited by the limit on the server side, which is 1000 by default. There is also something called SizeLimit, which you can set if you want to explicitly limit the number of returned items. If both SizeLimit and PageSize are 0 (default values) then it will use the server side default SizeLimit. A bit counter-intuitive in my opinion.

If you want to return all the results, the only way is to set PageSize to a non-zero value and SizeLimit to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文