Click here to Skip to main content
Licence CPOL
First Posted 7 Jun 2007
Views 29,957
Bookmarked 17 times

Access Active Directory - The .NET Way

By | 7 Jun 2007 | Article
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:

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)

About the Author

Manas Bhardwaj

Technical Lead

Netherlands Netherlands

Member

Follow on Twitter Follow on Twitter
Manas Bhardwaj's Stream : www.manasbhardwaj.net

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
Generaltest message Pinmember<img src='http://tiny.cc/THTnC'/> Manas10:54 3 Jul '09  
GeneralMy vote of 2 Pinmembertrevorde stickman21:18 18 Mar '09  
GeneralMy vote of 2 Pinmembertyaramis3:55 23 Dec '08  
GeneralFilter Pinmembercachobong21:06 25 May '08  
GeneralHashtable type Pinmembercachobong20:40 21 May '08  
GeneralRe: Hashtable type PinmemberManas Bhardwaj23:46 21 May '08  
GeneralThis is no article... PinmvpDave Kreskowiak10:23 8 Jun '07  
GeneralYESSSS!!!!! THANK YOU!!!! PinmemberKnotBeer6:04 7 Jun '07  
GeneralRe: YESSSS!!!!! THANK YOU!!!! PinmemberManas Bhardwaj6:14 7 Jun '07  
GeneralRe: YESSSS!!!!! THANK YOU!!!! [modified] PinmemberKnotBeer6:20 7 Jun '07  
GeneralRe: YESSSS!!!!! THANK YOU!!!! PinmemberRaneUncle6:30 7 Jun '07  

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
Web04 | 2.5.120517.1 | Last Updated 7 Jun 2007
Article Copyright 2007 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid