Its pretty simple. All ldap queries are pretty much the same. Just need to specify what properties to load and what objects to look for. If you want to find all objects its best to start the search from the root.
Function GetAllUsers()
Dim entry As DirectoryEntry = New DirectoryEntry(LDAPstr, LDAPuserName, LDAPpassword)
Try
Dim obj As Object = entry.NativeObject
Dim search As DirectorySearcher = New DirectorySearcher(entry)
search.Filter = "(&(objectCategory=Person))"
search.PropertiesToLoad.Add("name")
search.PropertiesToLoad.Add("samaccountname")
Dim gresult As SearchResultCollection = search.FindAll()
Dim result As SearchResult
If (gresult Is Nothing) Then
'no users found
Else
For Each result In gresult
'build a datatable and add each result as a new row to bind to a grid
response.write(result.Properties("name")(0).ToString & "<br>")
Next result
End If
Catch Ex as Exception
'an error occured
End Try
End Function
Marked as answer by Dino He - MSFT on Jun 06, 2012 10:11 AM
seageath
Member
31 Points
23 Posts
Get all user name from domain
May 30, 2012 02:00 AM|LINK
Hi all,
Is there any simple way / code to retrieve all user name from my existing domain using vb.net?
I want to display the user name and user full name, if possible in gridview.
Any help will be highly appreciated, meanwhile I try to generate my own code.
Thanks in advance,
BrockAllen
All-Star
27434 Points
4891 Posts
MVP
Re: Get all user name from domain
May 30, 2012 02:28 AM|LINK
This namespace & APIs should get you started.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
gww
Contributor
2143 Points
458 Posts
Re: Get all user name from domain
Jun 01, 2012 12:37 PM|LINK
Its pretty simple. All ldap queries are pretty much the same. Just need to specify what properties to load and what objects to look for. If you want to find all objects its best to start the search from the root.
Function GetAllUsers() Dim entry As DirectoryEntry = New DirectoryEntry(LDAPstr, LDAPuserName, LDAPpassword) Try Dim obj As Object = entry.NativeObject Dim search As DirectorySearcher = New DirectorySearcher(entry) search.Filter = "(&(objectCategory=Person))" search.PropertiesToLoad.Add("name") search.PropertiesToLoad.Add("samaccountname") Dim gresult As SearchResultCollection = search.FindAll() Dim result As SearchResult If (gresult Is Nothing) Then 'no users found Else For Each result In gresult 'build a datatable and add each result as a new row to bind to a grid response.write(result.Properties("name")(0).ToString & "<br>") Next result End If Catch Ex as Exception 'an error occured End Try End Function