简体   繁体   English

有没有一种快速的方法可以将所有用户从Active Directory中拉出?

[英]Is there a quick way to pull all of the users from Active Directory?

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

The call to FindAll goes back to the LDAP server every time. 每次对FindAll的调用都会返回到LDAP服务器。 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: 我并不是真的做VB.NET,但是这样的事情应该可以工作:

Dim searchResults = mySearcher.FindAll()
For Each item In searchResults
    userList.Add(item.Properties("DisplayName").Item(0))
Next

如果可以移至.NET 3.5,请尝试LINQ to Active Directory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM