Click here to Skip to main content
15,881,380 members
Articles / Web Development / ASP.NET
Article

Access Active Directory - The .NET Way

Rate me:
Please Sign up or sign in to vote.
2.67/5 (18 votes)
7 Jun 2007CPOL 50.1K   23   10
Accessing Active Directory

Introduction

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:

C#
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 +"))";

    mySearcher.PropertiesToLoad.Add("givenname");
    mySearcher.PropertiesToLoad.Add("sn");
    mySearcher.PropertiesToLoad.Add("mail");
    mySearcher.PropertiesToLoad.Add("description");
    mySearcher.PropertiesToLoad.Add("l");

    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;
}

History

  • 7th June, 2007: Initial post

License

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


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
GeneralMy vote of 2 Pin
trevorde stickman18-Mar-09 21:18
trevorde stickman18-Mar-09 21:18 
GeneralMy vote of 2 Pin
tyaramis23-Dec-08 3:55
tyaramis23-Dec-08 3:55 
GeneralFilter Pin
cachobong25-May-08 21:06
cachobong25-May-08 21:06 
GeneralHashtable type Pin
cachobong21-May-08 20:40
cachobong21-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
KnotBeer7-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
KnotBeer7-Jun-07 6:20 
GeneralRe: YESSSS!!!!! THANK YOU!!!! Pin
RaneUncle7-Jun-07 6:30
RaneUncle7-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.