Click here to Skip to main content
Licence 
First Posted 6 Oct 2004
Views 248,985
Bookmarked 52 times

Accessing LDAP User list using VB.NET

By | 6 Oct 2004 | Article
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:

 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:

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.

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

About the Author

Chandrashekhar Kulkarni

Web Developer

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiondemo ldap server? Pinmemberayman.metwally@gizasystems.com1:52 28 Apr '12  
GeneralHELP TO FIELDS PinmemberTONITACHIKILA8:30 19 Oct '07  
GeneralRe: HELP TO FIELDS PinmemberChandrashekhar Kulkarni5:55 21 Oct '07  
GeneralRe: HELP TO FIELDS PinmemberTONITACHIKILA9:04 22 Oct '07  
Questionnot working Pinmembereforr6:59 9 Aug '07  
QuestionGives an error,.....plz help PinmemberASysSolvers4:14 14 Jun '07  
GeneralGroups for a user. Pinmemberra-rag9:35 8 Jun '07  
GeneralLDAP PinmemberJagjot0:20 2 Apr '07  
GeneralRe: LDAP PinmemberChandrashekhar Kulkarni21:40 2 Apr '07  
GeneralListBox mulptiple selection Pinmemberbernie_01117:12 1 May '07  
QuestionHow to get userPassword? Pinmemberkiitenlpf21:28 18 Oct '06  
How to get userPassword?
AnswerRe: How to get userPassword? Pinmemberjhwurmbach21:30 18 Oct '06  
GeneralRe: How to get userPassword? Pinmemberkiitenlpf21:36 18 Oct '06  
QuestionHow to get a list of available properties ? PinmemberMatthieu.C5:24 8 Nov '05  
AnswerRe: How to get a list of available properties ? PinmemberSweet Angel10:34 27 Sep '06  
Generalldap query string Pinsussny3ranger7:38 29 Jun '05  
Generalgolbal searching for a string Pinmemberbrendanfry18:57 31 Jan '05  
GeneralRe: golbal searching for a string PinmemberChandrashekhar Kulkarni20:48 13 Apr '05  
GeneralLDAP & Groups PinmemberANORTON4:02 24 Jan '05  
GeneralRe: LDAP & Groups PinsussAnonymous18:11 24 Jan '05  
GeneralVB6.0 TO LDAP Pinsusssriramara7:17 19 Nov '04  
GeneralRe: VB6.0 TO LDAP PinmemberChandrashekhar Kulkarni0:23 29 Nov '04  
QuestionHow about "DirectorySearcher.Filter"? PinmemberBlackTigerAP17:25 20 Oct '04  
AnswerRe: How about "DirectorySearcher.Filter"? PinmemberChandrashekhar Kulkarni23:06 24 Oct '04  
General"Multi-value" properties PinmemberBlackTigerAP17:21 20 Oct '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 6 Oct 2004
Article Copyright 2004 by Chandrashekhar Kulkarni
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid