Click here to Skip to main content
14,332,426 members

Access Active Directory - The .NET Way

Rate this:
2.67 (18 votes)
Please Sign up or sign in to vote.
2.67 (18 votes)
7 Jun 2007CPOL
Accessing Active Directory


Accessing Active Director (AD) is one of the most basic scenarios for an intranet application. For a new developer though, sometimes it becomes clumsy to get this. But believe me, it is just as simple as hitting a full toss (shows how much I love cricket).

Using the Code

The following code shows how to do it with C#. Remember to include System.DirectoryServices. Here you go:

public static Hashtable SearchLDAP(string userID)
    DirectoryEntry entry = new DirectoryEntry("LDAP://DC=Domain,DC=com");
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(&(objectClass=user)(anr="+ userID +"))";


    Hashtable associateDetailsTable = new Hashtable();
    ResultPropertyValueCollection resultCollection;
    SearchResult searchResult = mySearcher.FindOne();

    associateDetailsTable.Add("AssociateID", userID);
    if(searchResult != null)
    resultCollection = searchResult.Properties["givenname"];
    foreach(object result in resultCollection)
    associateDetailsTable.Add("FirstName", result.ToString());
    resultCollection = searchResult.Properties["sn"];
    foreach(object result in resultCollection)
    associateDetailsTable.Add("LastName", result.ToString());
    resultCollection = searchResult.Properties["mail"];
    foreach(object result in resultCollection)
    associateDetailsTable.Add("Mail", result.ToString());
    resultCollection = searchResult.Properties["description"];
    foreach(object result in resultCollection)
    associateDetailsTable.Add("Designation", result.ToString());
    resultCollection = searchResult.Properties["l"];
    foreach(object result in resultCollection)
    associateDetailsTable.Add("Location", result.ToString());
    return associateDetailsTable;


  • 7th June, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manas Bhardwaj
Netherlands Netherlands

Read my personal blog at

Comments and Discussions

GeneralMy vote of 2 Pin
trevorde stickman18-Mar-09 21:18
membertrevorde stickman18-Mar-09 21:18 
GeneralMy vote of 2 Pin
tyaramis23-Dec-08 3:55
membertyaramis23-Dec-08 3:55 
GeneralFilter Pin
cachobong25-May-08 21:06
membercachobong25-May-08 21:06 
GeneralHashtable type Pin
cachobong21-May-08 20:40
membercachobong21-May-08 20:40 
GeneralRe: Hashtable type Pin
Manas Bhardwaj21-May-08 23:46
professionalManas Bhardwaj21-May-08 23:46 
GeneralThis is no article... Pin
Dave Kreskowiak8-Jun-07 10:23
mveDave Kreskowiak8-Jun-07 10:23 
GeneralYESSSS!!!!! THANK YOU!!!! Pin
KnotBeer7-Jun-07 6:04
memberKnotBeer7-Jun-07 6:04 
GeneralRe: YESSSS!!!!! THANK YOU!!!! Pin
Manas Bhardwaj7-Jun-07 6:14
professionalManas Bhardwaj7-Jun-07 6:14 
GeneralRe: YESSSS!!!!! THANK YOU!!!! [modified] Pin
KnotBeer7-Jun-07 6:20
memberKnotBeer7-Jun-07 6:20 
GeneralRe: YESSSS!!!!! THANK YOU!!!! Pin
RaneUncle7-Jun-07 6:30
memberRaneUncle7-Jun-07 6:30 

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.

Posted 7 Jun 2007


23 bookmarked