有没有一种快速方法可以从 Active Directory 中提取所有用户?
我正在尝试通过活动目录提取每个可用用户的用户名。这是我的同事首先尝试使用的代码,但这种方法会烧掉所有内存并抛出内存不足异常。有没有快速的替代方案?
Dim userList As ArrayList = New ArrayList
Dim sPath As String = "LDAP://test.ca/OU=foo,OU=bar,OU=foobar,DC=test,DC=ca"
Dim myDirectory As New DirectoryEntry(sPath, Nothing, Nothing, AuthenticationTypes.Secure)
Dim mySearcher As New DirectorySearcher(myDirectory)
mySearcher.Filter = ("(objectClass=user)")
For i As Integer = 0 To mySearcher.FindAll().Count - 1
userList.Add(mySearcher.FindAll.Item(i).Properties("DisplayName").Item(0))
Next
I'm trying to pull the username of every user available through active directory. Here is the code my colleague first tried to use, but this method is burning all of the memory out and throwing out of memory exceptions. Is there a quick alternative?
Dim userList As ArrayList = New ArrayList
Dim sPath As String = "LDAP://test.ca/OU=foo,OU=bar,OU=foobar,DC=test,DC=ca"
Dim myDirectory As New DirectoryEntry(sPath, Nothing, Nothing, AuthenticationTypes.Secure)
Dim mySearcher As New DirectorySearcher(myDirectory)
mySearcher.Filter = ("(objectClass=user)")
For i As Integer = 0 To mySearcher.FindAll().Count - 1
userList.Add(mySearcher.FindAll.Item(i).Properties("DisplayName").Item(0))
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次对
FindAll
的调用都会返回到 LDAP 服务器。这意味着每次循环时您都会执行它(并锤击服务器)。最重要的是,如果数据在调用之间发生变化,您可能会看到一些非常奇怪(且难以诊断)的错误。我不太擅长 VB.NET,但这样的东西应该可以工作:
The call to
FindAll
goes back to the LDAP server every time. This means that you're executing it (and hammering the server) every time you go round the loop. On top of that, if the data changes between calls, you'll probably see some really odd (and hard to diagnose) bugs.I don't really do VB.NET, but something like this should work:
如果您可以迁移到 .NET 3.5,请尝试 LINQ to Active Directory。
If you can move to .NET 3.5, try LINQ to Active Directory.