有没有一种快速方法可以从 Active Directory 中提取所有用户?

发布于 2024-08-13 13:42:42 字数 552 浏览 4 评论 0原文

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

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 技术交流群。

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

发布评论

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

评论(2

一页 2024-08-20 13:42:42

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

我不太擅长 VB.NET,但这样的东西应该可以工作:

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

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:

Dim searchResults = mySearcher.FindAll()
For Each item In searchResults
    userList.Add(item.Properties("DisplayName").Item(0))
Next
千仐 2024-08-20 13:42:42

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

If you can move to .NET 3.5, try LINQ to Active Directory.

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