Click here to Skip to main content
Licence 
First Posted 29 Jul 2004
Views 111,063
Bookmarked 55 times

Enumerating Active Directory Objects using C#

By | 29 Jul 2004 | Article
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/.

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.

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:

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.

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

About the Author

umair_hasan



Pakistan Pakistan

Member



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
GeneralExcellent!! PinmemberAntonio Urbano3:31 11 Apr '11  
GeneralGreat! PinmemberMurat DOGANCAY4:54 9 Jan '10  
GeneralGood Pinmemberzahkoonkoo0:40 30 Sep '09  
GeneralThanks!! Pinmemberpechan000009:02 26 Jul '09  
QuestionNeed help, AD & (VB or C#) PinmemberhoshyarD1:29 7 Jan '09  
GeneralThank you!! PinmemberIAmRami5:08 21 Dec '08  
GeneralLimits PinmemberTCHamilton7:53 26 Sep '08  
Generalfiles are empty Pinmemberjseghers1:10 8 Sep '08  
GeneralDirectoryServicesCOMException Occures. PinmemberkansalSunny0:34 24 Nov '06  
GeneralVery Nice PinmemberSled Dog13:37 12 Nov '06  
QuestionWorking in WebProject Pinmemberyuki108:21 25 Apr '06  
Generaliterating properties that are collections (PropertyValueCollection ) i.e. member Pinmemberssepan4:34 10 Mar '06  
GeneralGood Pinmemberram kinkar pandey19:28 15 Jul '05  
GeneralConfused PinmemberConfused++++8:58 12 Aug '04  
GeneralMake it even easier... PinmemberMarc Scheuner3:24 30 Jul '04  
GeneralRe: Make it even easier... Pinmember4r3s9:07 24 Mar '08  
GeneralRe: Make it even easier... PinmemberMarc Scheuner10:34 24 Mar '08  
GeneralRe: Make it even easier... [modified] Pinmember4r3s2:36 25 Mar '08  

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
Web01 | 2.5.120517.1 | Last Updated 30 Jul 2004
Article Copyright 2004 by umair_hasan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid