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

Simple Active Directory Browser

Rate me:
Please Sign up or sign in to vote.
4.30/5 (23 votes)
16 Dec 2003CPOL 172.6K   4.8K   43   29
A simple Active Directory Browser for beginners.

Sample Image - ActiveDirectoryBrowser.jpg

Introduction

Ever wondered how to write a simple Active Directory Browser? Well, then look at my example code here. Its quite simple and straightforward. I am utilizing System.DirectoryServices to connect to AD and enumerate objects.

Of course, this is a simple example which doesn't include threading to make the user interface respond faster. But it will give you an idea of how to connect to AD and do some simple browsing.

I also used my listview column sorter class, which is available here.

Code Basics

To connect to Active Directory is quite simple:

C#
// connect to active directory 
DirectoryEntry rootDSE = new 
  DirectoryEntry("LDAP<A href="ldap://rootDSE/">://rootDSE/</A>"); 
DirectoryEntry root = new DirectoryEntry("LDAP://" + 
  (string)rootDSE.Properties["defaultNamingContext"].Value);

The property defaultNamingContext contains the domain name in format: "dc=test,dc=com" (for domain name "test.com") which we use to connect to the test.com domain.

History

  • 2003-11-04 - First edition, more updates will come later.
  • 2003-11-13 - Dominic found a bug. I uploaded a new example project that fixes the bug.

License

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


Written By
Software Developer (Senior) a large company
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Domains Pin
sevenstorydrop16-Nov-03 13:36
sevenstorydrop16-Nov-03 13:36 
GeneralRe: Domains Pin
Sven So.17-Nov-03 19:40
Sven So.17-Nov-03 19:40 
GeneralRe: Domains Pin
dromano1110-Mar-05 10:40
dromano1110-Mar-05 10:40 
GeneralSorting columns error Pin
jsk647814-Nov-03 3:40
jsk647814-Nov-03 3:40 
GeneralRe: Sorting columns error Pin
Sven So.17-Nov-03 19:38
Sven So.17-Nov-03 19:38 
GeneralRe: Sorting columns error Pin
jsk647818-Dec-03 5:38
jsk647818-Dec-03 5:38 
GeneralCan't get it to work Pin
Domenic12-Nov-03 7:09
Domenic12-Nov-03 7:09 
GeneralRe: Can't get it to work Pin
Sven So.12-Nov-03 20:50
Sven So.12-Nov-03 20:50 
If you use AD (Active Directory) than it should just work fine, because its made for it. ADSI (Active Directory Services Interface) supports 2 providers one is LDAP, which you could also use to connect to other LDAP directories (Active Directory is also an LDAP directory). Winnt is mainly used to connect to NT4 domains, but can still be used to access Active Directory for backwards compatibility. In your case Winnnt doesnt has such property defaultNamingContext. If your computer is in Active Directory and you are logged on with a user from AD my example should just work fine. With the call to the rootDSE he will get information about your AD domain, and then in the second call connect to AD with defaultNamingContext, e.g. "dc=test,dc=com". Just try the example without changing anything. Please come back, if it still doesnt work for you.
GeneralRe: Can't get it to work Pin
Domenic13-Nov-03 2:23
Domenic13-Nov-03 2:23 
GeneralRe: Can't get it to work Pin
Sven So.13-Nov-03 2:35
Sven So.13-Nov-03 2:35 

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.