views:

81

answers:

2

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
+1  A: 

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

CesarGon
+2  A: 

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
Roger Lipscombe