Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / Visual Basic
Article

Accessing LDAP User list using VB.NET

Rate me:
Please Sign up or sign in to vote.
4.00/5 (10 votes)
6 Oct 20042 min read 407.4K   57   35
This article will explain how to access LDAP service using VB.NET.

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:

VB
 Public Function GetAllUsers(ByVal ldapServerName As String) As Hashtable 

 'To retrieve list of all  LDAP users 

 'This function returns 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

  'MsgBox("Error is " & e.Message)
  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:

VB
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:

VB
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.

VB
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:

VB
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Web Development:
.Net 2.0, VB.net, ASP.net, Classic ASP, VBScript, Javascript, HTML, DHTML,

OpenSource:
Perl, PHP, Linux, Apache, IIS.

Desktop Developement:
VB.net, VB 6, COM/DCOM, Winsock, MAPI, CDO, Outlook Object Model, LDAP, C, C++, Foxpro 2.5, Clipper.

Database:
MS SQL - Server, MySQL, PostgreSQL

Other:
Enterprise Architect, Microsoft Project, Rational Rose, UML.

Has Nine+ years of experience with IT and now working in Project Manager's Role.

Comments and Discussions

 
QuestionSecuring Password Pin
Member 1358132518-Dec-17 2:46
Member 1358132518-Dec-17 2:46 
Questiondemo ldap server? Pin
ayman.metwally@gizasystems.com28-Apr-12 1:52
ayman.metwally@gizasystems.com28-Apr-12 1:52 
GeneralHELP TO FIELDS Pin
TONITACHIKILA19-Oct-07 8:30
TONITACHIKILA19-Oct-07 8:30 
GeneralRe: HELP TO FIELDS Pin
Chandrashekhar Kulkarni21-Oct-07 5:55
Chandrashekhar Kulkarni21-Oct-07 5:55 
GeneralRe: HELP TO FIELDS Pin
TONITACHIKILA22-Oct-07 9:04
TONITACHIKILA22-Oct-07 9:04 
Questionnot working Pin
eforr9-Aug-07 6:59
eforr9-Aug-07 6:59 
QuestionGives an error,.....plz help Pin
ASysSolvers14-Jun-07 4:14
ASysSolvers14-Jun-07 4:14 
GeneralGroups for a user. Pin
ra-rag8-Jun-07 9:35
ra-rag8-Jun-07 9:35 
GeneralLDAP Pin
Jagjot2-Apr-07 0:20
Jagjot2-Apr-07 0:20 
GeneralRe: LDAP Pin
Chandrashekhar Kulkarni2-Apr-07 21:40
Chandrashekhar Kulkarni2-Apr-07 21:40 
GeneralListBox mulptiple selection Pin
bernie_0111-May-07 17:12
bernie_0111-May-07 17:12 
GeneralRe: LDAP Pin
Cyber Friend5-Jun-12 22:51
Cyber Friend5-Jun-12 22:51 
QuestionHow to get userPassword? Pin
kiitenlpf18-Oct-06 21:28
kiitenlpf18-Oct-06 21:28 
AnswerRe: How to get userPassword? Pin
jhwurmbach18-Oct-06 21:30
jhwurmbach18-Oct-06 21:30 
GeneralRe: How to get userPassword? Pin
kiitenlpf18-Oct-06 21:36
kiitenlpf18-Oct-06 21:36 
GeneralRe: How to get userPassword? Pin
Cyber Friend5-Jun-12 22:48
Cyber Friend5-Jun-12 22:48 
QuestionHow to get a list of available properties ? Pin
Matthieu.C8-Nov-05 5:24
Matthieu.C8-Nov-05 5:24 
AnswerRe: How to get a list of available properties ? Pin
Sweet Angel27-Sep-06 10:34
Sweet Angel27-Sep-06 10:34 
Generalldap query string Pin
ny3ranger29-Jun-05 7:38
ny3ranger29-Jun-05 7:38 
Generalgolbal searching for a string Pin
brendanfry31-Jan-05 18:57
brendanfry31-Jan-05 18:57 
GeneralRe: golbal searching for a string Pin
Chandrashekhar Kulkarni13-Apr-05 20:48
Chandrashekhar Kulkarni13-Apr-05 20:48 
GeneralLDAP & Groups Pin
ANORTON24-Jan-05 4:02
ANORTON24-Jan-05 4:02 
GeneralRe: LDAP & Groups Pin
Anonymous24-Jan-05 18:11
Anonymous24-Jan-05 18:11 
GeneralVB6.0 TO LDAP Pin
sriraman raman19-Nov-04 7:17
sriraman raman19-Nov-04 7:17 
GeneralRe: VB6.0 TO LDAP Pin
Chandrashekhar Kulkarni29-Nov-04 0:23
Chandrashekhar Kulkarni29-Nov-04 0:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.