如果一个OU包含3000个用户,如何使用DirectorySearcher找到所有用户?
我使用这段代码:
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 个用户。
我怎样才能找到所有这些?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 .NET 3.5 或更高版本,您应该查看
PrincipalSearcher
和“按示例查询”主体来进行搜索:如果您还没有 - 绝对要阅读 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: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 nameUser Principal Name
- your "[email protected]" style nameYou can specify any of the properties on the
UserPrincipal
and use those as "query-by-example" for yourPrincipalSearcher
.Update #2: If you want to search just inside a given OU, you can define that OU in the constructor of the
PrincipalContext
.您需要设置 DirectorySearcher.PageSize 属性能够返回所有结果。例如:
否则返回的项目数量将受到服务器端的限制,默认为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:
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.