Introduction
Reading list of all LDAP users
Compared to VB 6.0, .NET framework has given very easy access to the network solutions like LDAP. I have seen lots of people asking questions on LDAP access using .NET. In this article, I will try to explain how to retrieve list of all LDAP users.
Code:
Public Function GetAllUsers(ByVal ldapServerName As String) As Hashtable
_ldapServerName = ldapServerName
Dim sServerName As String = "mail"
Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _
"/ou=People,dc=mydomainname,dc=com")
Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
Dim oResults As SearchResultCollection
Dim oResult As SearchResult
Dim RetArray As New Hashtable()
Try
oSearcher.PropertiesToLoad.Add("uid")
oSearcher.PropertiesToLoad.Add("givenname")
oSearcher.PropertiesToLoad.Add("cn")
oResults = oSearcher.FindAll
For Each oResult In oResults
If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
RetArray.Add( oResult.GetDirectoryEntry().Properties("uid").Value, _
oResult.GetDirectoryEntry().Properties("cn").Value)
End If
Next
Catch e As Exception
Return RetArray
End Try
Return RetArray
End Function
Details:
As a basic, when we are writing applications that are related with LDAP, we need to take reference to the System.DirectoryServices
namespace. To add the reference, just right click on the project and select "Add References". This will present the interface to select the .NET components that can be referred in the project. In this list, select System.DirectoryServices.dll and click Add. Now, in the project, open the form and add the following line at the top:
Imports System.DirectoryServices
After doing this operation, System.DirectoryServices
is accessible in the application.
LDAP Implementation:
Normally, all elements and objects of LDAP are stored in a tree structure. To access this tree structure, we need to have a root element using which we can iterate through all child elements.
Obtaining a Root Element of LDAP:
Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & _
ldapServerName & "/ou=People,dc=mydomainname,dc=com")
Using this line, we can obtain the root of the LDAP tree structure.
Now, next job is to find all the entries of users from the LDAP tree. For this search operation, .NET Framework has provided a class, i.e. DirectorySearcher
.
Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
This class expects a parameter of DirectoryEntry
and returns data in SearchResultCollection
.
To access the SearchResultCollection
, we need to use SearchResult
object. The search result will contain the fields that we have specified in the load properties. To specify which property is to be loaded, we need to pass the field name as a string to the PropertiesToLoad
method of the searcher object.
For example:
oSearcher.PropertiesToLoad.Add("givenname")
Make sure that you specify correct field names.
Now, the FindAll
method of the object searcher will return the SearchResults
collection. This collection will contain SearchResult
(as specified above) and will have directory entries with the loaded properties.
In this example I have put all the values in a HashTable
with Unique ID (UID
) as key and Common Name (cn
) as value.