【问题标题】:Is there a quick way to pull all of the users from Active Directory?有没有一种快速的方法可以从 Active Directory 中提取所有用户?
【发布时间】:2009-12-07 18:33:15
【问题描述】:

我正在尝试通过活动目录提取每个可用用户的用户名。这是我同事第一次尝试使用的代码,但是这种方法正在烧毁所有内存并抛出内存异常。有没有快速的替代方案?

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

【问题讨论】:

  • 我没有在 AD 中做过,但是在 LDAP 中,告诉 LDAP 你只想要返回的显示名称,这将是一个较短的列表。

标签: c# vb.net .net-2.0 active-directory


【解决方案1】:

FindAll 的调用每次都会返回到 LDAP 服务器。这意味着您每次循环时都在执行它(并锤击服务器)。最重要的是,如果数据在调用之间发生变化,您可能会看到一些非常奇怪(且难以诊断)的错误。

我并没有真正做 VB.NET,但是这样的东西应该可以工作:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 2013-05-15
      • 2020-02-27
      相关资源
      最近更新 更多