Click here to Skip to main content
13,301,100 members (81,754 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 26 Jan 2011

Accessing Active Directory Objects via C# (Visual Studio)

, 9 Feb 2011
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Abhijit Shiposkar
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThanks! Pin
Oshadac28-Jun-13 22:36
memberOshadac28-Jun-13 22:36 
GeneralHi, How to list all users in ListBox include cn, mobile, ma... Pin
Member 356807622-Feb-11 3:03
memberMember 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
memberMarc 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
memberPranay 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 10 Feb 2011
Article Copyright 2011 by Abhijit Shiposkar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid