Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Article

Enumerating Active Directory Objects using C#

Rate me:
Please Sign up or sign in to vote.
4.50/5 (19 votes)
29 Jul 20042 min read 157.5K   5.2K   63   19
This article shows how to test an Active Directory connection and enumerate all objects based upon some filters.

Sample Image - ADTester_Image.jpg

Introduction

Microsoft introduced the concept of Active Directory with Windows 2000. Information about various resources like people, machines, printers, groups etc. are stored in Active Directory. It provides a single point of management for Windows-based user accounts, clients, servers, and applications, and facilitates network administrators and users to have an integrated view of a network.

This simple example uses System.DirectoryServices namespace to interact with Active Directory. This namespace contains two classes DirectoryEntry and DirectorySearcher to connect to and to retrieve data from an Active Directory source. These classes can be used with any of the four AD service providers, LDAP, IIS, NDS, and WinNT. I have used LDAP as it's a text based, easy to use, and platform independent protocol.

User can specify a specific AD source path or otherwise default AD would be queried. Different Filters can be applied to list the directory objects like object categories (users, computers, groups etc.) and Organizational units (OUs).

In order to interact with an active directory, you have to first call directory entry constructor which takes an LDAP string (in this case) as a parameter. This LDAP string can contain the name of domain controllers and Organizational units like: ldap://OU=IT,DC=12,DC=test,DC=com/.

C#
DirectoryEntry entry = null;
entry = new DirectoryEntry(strPath);

where strPath contains the LDAP string path.

To retrieve directory objects from an Active Directory, you have to create an object of DirectorySearcher class and pass DirectoryEntry's objects as a parameter in its constructor.

C#
DirectorySearcher mySearcher = new DirectorySearcher(entry);

DirectorySearcher provides different properties to perform a customized search. You can create a custom filter to perform a filtered search like:

C#
mySearcher.Filter = "(ObjectCategory=user");

Finally, it has a function FindAll() which searches the directory for desired results and returns a collection of nodes from Active Directory of type SearchResultCollection. You can iterate through the collection to get individual results.

C#
foreach(SearchResult result in mySearcher.FindAll()) 
{
  strName = result.GetDirectoryEntry().Name;
  //Do whatever
}

I have written a function GetLDAPPath() which returns a formatted LDAP string based on the input user has specified.

Hope you will enjoy this small sample!

License

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


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

Comments and Discussions

 
GeneralMy vote of 3 Pin
niland.mark6-Feb-13 23:58
niland.mark6-Feb-13 23:58 
GeneralExcellent!! Pin
Antonio Urbano11-Apr-11 3:31
Antonio Urbano11-Apr-11 3:31 
GeneralGreat! Pin
Murat DOGANCAY9-Jan-10 4:54
Murat DOGANCAY9-Jan-10 4:54 
GeneralGood Pin
zahkoonkoo30-Sep-09 0:40
zahkoonkoo30-Sep-09 0:40 
Thanx for this code, it is very useful for me Poke tongue | ;-P
GeneralThanks!! Pin
pechan0000026-Jul-09 9:02
pechan0000026-Jul-09 9:02 
QuestionNeed help, AD & (VB or C#) Pin
hoshyarD7-Jan-09 1:29
hoshyarD7-Jan-09 1:29 
GeneralThank you!! Pin
IAmRami21-Dec-08 5:08
IAmRami21-Dec-08 5:08 
GeneralLimits Pin
TCHamilton26-Sep-08 7:53
TCHamilton26-Sep-08 7:53 
Generalfiles are empty Pin
jseghers8-Sep-08 1:10
jseghers8-Sep-08 1:10 
GeneralDirectoryServicesCOMException Occures. Pin
kansalSunny24-Nov-06 0:34
kansalSunny24-Nov-06 0:34 
GeneralVery Nice Pin
Sled Dog12-Nov-06 13:37
Sled Dog12-Nov-06 13:37 
QuestionWorking in WebProject Pin
yuki1025-Apr-06 8:21
yuki1025-Apr-06 8:21 
Generaliterating properties that are collections (PropertyValueCollection ) i.e. member Pin
ssepan10-Mar-06 4:34
ssepan10-Mar-06 4:34 
GeneralGood Pin
ram kinkar pandey15-Jul-05 19:28
ram kinkar pandey15-Jul-05 19:28 
GeneralConfused Pin
Member 92539912-Aug-04 8:58
Member 92539912-Aug-04 8:58 
GeneralMake it even easier... Pin
Marc Scheuner30-Jul-04 3:24
professionalMarc Scheuner30-Jul-04 3:24 
GeneralRe: Make it even easier... Pin
4r3s24-Mar-08 9:07
4r3s24-Mar-08 9:07 
GeneralRe: Make it even easier... Pin
Marc Scheuner24-Mar-08 10:34
professionalMarc Scheuner24-Mar-08 10:34 
GeneralRe: Make it even easier... [modified] Pin
4r3s25-Mar-08 2:36
4r3s25-Mar-08 2:36 

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.