Click here to Skip to main content
15,565,995 members
Articles / Programming Languages / C#
Posted 26 Jan 2011


17 bookmarked

Accessing Active Directory Objects via C# (Visual Studio)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
9 Feb 2011CPOL
Accessing Active Directory Objects via C# (Visual Studio)

This is an article for a beginner to learn how to access Active directory objects via C# code.

Using the Code

To use the code, you would need a Windows server with Active Directory installed and Visual Studio on your machine.

System References

Make sure you have included the following namespaces in your code:

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

Directory Entry Object

Now we create a directory entry object for our Active directory.

DirectoryEntry dir = new DirectoryEntry("LDAP://your_domain_name");

Creating a Search Object and Executing the Search

The DirectorySearcher object searches the Active directory. You can set the filter property to retrieve specific records. I am also using the AND "&" property to combine two conditions.

DirectorySearcher search = new DirectorySearcher(dir);

search.Filter = "(&(objectClass=user)(givenname=First_Name))";

Handling Search Results

Firstly, we create a SearchResult object to get the data from the search. Next, I have shown how to get all the property names for that object, which can be later used to get any particular property value. Finally, we get the directory entry from the Search Result and then specify a particular property name to get its value.

SearchResult searchresult = search.FindOne(); // You can also use the FindAll() method for multiple objects.

   if (searchresult != null)
	foreach(System.Collections.DictionaryEntry direntry in searchresult.Properties) 
                    TextBox1.Text += direntry.Key.ToString() +"\n"; // This will give you all the property names that are set for that particular object,which you can use to get a specific value.I have used a Multiline textbox here.			      

        TextBox1.Text += searchresult.GetDirectoryEntry().Properties["sn"].Value.ToString(); // I am displaying the lastname/surname in simple textbox.

Points of Interest

I hope this article will help beginners to get to know how to access Active Directory objects through C# code.


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

Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralThanks! Pin
Oshadac28-Jun-13 22:36
Oshadac28-Jun-13 22:36 
GeneralHi, How to list all users in ListBox include cn, mobile, ma... Pin
Member 356807622-Feb-11 3:03
Member 356807622-Feb-11 3:03 
GeneralFor .NET 3.5 and up, I would recommend having a look at the ... Pin
Marc Scheuner11-Feb-11 1:03
professionalMarc Scheuner11-Feb-11 1:03 
GeneralReason for my vote of 5 thanks for sharing- have 5 , just fo... Pin
Pranay Rana26-Jan-11 21:46
professionalPranay Rana26-Jan-11 21:46 

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.