Click here to Skip to main content
Click here to Skip to main content

How to get User Data from the Active Directory

, 20 Apr 2004
Rate this:
Please Sign up or sign in to vote.
How to get User Data from the Active Directory using C#


Last month the project manager asked me write to find all users information from the Active directory and which all fields are missing information for particular user. I was trying to search in Internet for information about .NET Active Directory examples, I could not able to find much information on the net, it prompted me write an article on this topic.

In this article, I will explain how to use Active Directory class and retrieve data from the component classes. You can cut and past below code lines and execute it but you need to pass domain name in Directory Entry constructor. Following example taken from one of my developed projects and modified for easy to understand.

I assumed that you have a general understanding of active directory before using this example.

Step 1:

Add System.DirectoryServices.Dll (from Project Add reference)

System.DirectoryServices provides easy access to active directory from managed code. This namespace contains two components classes, DirectoryEntry and DirectorySearcher.

Step 2:

Using System.DirectoryServices

Directory Entry Class: this class encapsulates a node or object in the active directory hierarchy. Use this class for binding to objects, reading properties and updating attributes.

Step 3:

DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName");

Directory Searcher: It will perform queries against the active directory hierarchy

Step 4:

DirectorySearcher Dsearch = new DirectorySearcher(entry);

Step 5:

String Name="Richmond";

The Filter property supports for the all filter the information of the active directory.

// l = city name

Step 6:

dSearch.Filter = "(&(objectClass=user)(l=" + Name + "))";

Executes the search and returns a collection of the entries that are found.

Step 7:

This function checks active directory field is valid or not. Add this member function to you class.

Public static string GetProperty(SearchResult searchResult, 
 string PropertyName)
    return searchResult.Properties[PropertyName][0].ToString() ;
    return string.Empty;

Step 8:

// get all entries from the active directory.
// Last Name, name, initial, homepostaladdress, title, company etc..
 foreach(SearchResult sResultSet in dSearch.FindAll())
     // Login Name
     // First Name
     // Middle Initials
     // Last Name
     // Address
 string tempAddress=GetProperty(sResultSet,"homePostalAddress");

     if(tempAddress !=string.Empty)
          string[] addressArray = tempAddress.Split(';');
      string taddr1,taddr2;
     // title
     // company
     //postal code
     // telephonenumber
    // email address
     // Challenge Question
     // Challenge Response
     //Member Company
     // Company Relation ship Exits
          // Assigned Sales Person
     // Accept T and C
     // jobs
   String tEamil = GetProperty(sResultSet,"extensionAttribute9");
     // email over night
      string em1,em2,em3;
    string[] emailArray = tEmail.Split(';');
     // email daily emerging market
     // email daily corporate market
     // AssetMgt Range
     // date of account created
   // date of account changed



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

rajasekhara sambangi

United States United States
Rajasekhara sambangi has 8 years experience in
software development .
Developing .NET,ASP.NET,C#,VB.NET,SQLSERVER AND ORACLE Projects from last 4 years.

Comments and Discussions

GeneralMy vote of 5 Pinmembergaurav gujral18-Nov-14 18:28 
GeneralMy vote of 2 PinmemberMember 17933028-Nov-12 8:19 
SuggestionNice article PinmemberSHAJANCHERIAN7-Aug-12 21:01 
QuestionLogon failure: unknown user name or bad password. Pinmembermurugesan.velusamy14-Jun-12 22:22 
I have used the same method to fetch detail of users in different domain. when i am using the tool in one of our domain which encounters with error, I am attaching the error message and code below
error =>"Logon failure: unknown user name or bad password."
string strName;
strName = Request.ServerVariables["AUTH_USER"];
string[] UserDetail = strName.Split('\\');
string Str = "LDAP://" + UserDetail[0].ToString().ToUpper();
DirectoryEntry entry = new DirectoryEntry(Str);
DirectorySearcher Dsearch = new DirectorySearcher(entry);
Dsearch.Filter = "(sAMAccountName=" + UserDetail[1].ToString().ToUpper() + ")";
SearchResult sresult = Dsearch.FindOne();
DirectoryEntry dsresult = sresult.GetDirectoryEntry();
lblfname.Text = dsresult.Properties["givenName"][0].ToString();
lbllname.Text = dsresult.Properties["sn"][0].ToString();
AnswerRe: Logon failure: unknown user name or bad password. PinmemberMember 115155628-Jun-12 23:29 
Questionnice one Pinmemberyasir1152-May-12 20:27 
GeneralMy vote of 2 Pinmembervinakar13-Dec-11 3:06 
GeneralRetrieving the users from Active Directory PinmemberMember 78048561-Apr-11 4:10 
Questionwhere is the all code in zip ?? Pinmemberalhambra-eidos28-Dec-10 0:32 
GeneralMy vote of 1 Pinmembersam_mj8-Jul-10 4:44 
GeneralRe: My vote of 1 PinmemberDjValy19-Oct-10 17:37 
GeneralGetting Active Directory Groups from User Account PinmemberMember 323276622-Apr-09 3:23 
QuestionHi this is phani Pinmembermaganti@phani24-Mar-09 20:58 
Generaleasy application for active directory data retrieval PinmemberMoustafa Deif2-Dec-08 2:00 
GeneralGet currently logged in user list for specific machine using its IP address PinmemberPranjaliBhide3-Jul-08 5:30 
GeneralRe: Get currently logged in user list for specific machine using its IP address Pinmemberaaron vicuna10-Jan-09 18:29 
GeneralRe: Get currently logged in user list for specific machine using its IP address PinmemberPranjaliBhide17-Feb-09 21:23 
GeneralFind 'null' values Pinmemberkeiyia13-Nov-07 11:12 
GeneralStep back PinmemberDan Stoll22-Oct-07 20:02 
AnswerRe: Step back PinmemberSpasticus3-Aug-10 3:58 
GeneralVery Great Article PinmemberIyad Abu Abdu9-Sep-07 7:06 
QuestionQuestion Pinmemberkhan_SharePoint23-Jul-07 6:38 
GeneralGreat example Pinmemberalzikan9-Jul-07 8:00 
GeneralAwesome example Thank you! Pinmemberdberecha2-Apr-07 11:07 
Generalbugs bugs bugs Pinmemberphm3713-Feb-07 6:28 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 21 Apr 2004
Article Copyright 2004 by rajasekhara sambangi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid