Click here to Skip to main content
15,114,081 members
Articles / Programming Languages / C#
Posted 10 Apr 2010

Tagged as


54 bookmarked

Active Directory Helper

Rate me:
Please Sign up or sign in to vote.
4.88/5 (10 votes)
10 Apr 2010CPOL2 min read
A small set of classes for simplifying the access to Windows Active Directory


This article shows you some small classes that you can use to query the Microsoft Active Directory (AD) for users and groups and perform other user and group related functions.


I developed these classes because I wanted to have a simple to use interface to query users and groups when importing them into our free test case management tool called "Zeta Test".

Internally, the library makes use of the classes in the System.DirectoryServices namespace which provide LDAP functions to access an Active Directory. So everything that I provide here can be used without my libraries, too. The reason to develop it was to simplify the access to the underlying classes, which I found hard to understand and use in the past.

These classes are also available together with other general-purpose classes through my Zeta Enterprise Library article.

Provided Classes

Basically, you have the following classes inside the library:

  • ActiveDirectoryConfiguration - Contains configuration settings for accessing the LDAP server like server name, user name, password, impersonation, DN, etc.
  • ActiveDirectory - Central class to execute certain AD functions like enumerating users and groups.
  • ADUserInfo - Class containing information about one AD user.
  • ADGroupInfo - Class containing information about one AD group.

Besides these classes, there are some helper classes (see "Helper" sub-folder in the sources) and some enumerations. The download also contains a project with some unit tests.

Using the Code

The usage of the code should be rather simple. Following is a short example:

var adc =
    new ActiveDirectoryConfiguration
        LdapServer = "MyServerNameOrIP",
        LdapBaseDN = "dc=office, dc=my-domain, dc=com",
        LdapUserName = "MYDOMAIN\\myuser",
        LdapPassword = "mypassword"

var ad = new ActiveDirectory(adc);

In that example, a new instance of the ActiveDirectoryConfiguration class is being created, filled with connection values and then passed to the constructor of an ActiveDirectory class.

Next, you can call methods on this instance:

var allGroups = ad.GetGroupInfos();
var allUsers = ad.GetUserInfos();

Here, we retrieve a list of all groups and all users inside the DN, specified in the configuration above.

To access the retrieved information, we can e.g., iterate through the retrieved lists and call members on each object:

if (allGroups != null)
    foreach (var group in allGroups)

if (allUsers != null)
    foreach (var user in allUsers)

This example simply traces the name of each user group and each user to the trace listeners.


This article quickly introduced some classes to query the Microsoft LDAP ActiveDirectory through an easy to use interface. To get these classes together with much more functions in a small set of libraries, please see my Zeta Enterprise Library article.

If you have any questions, comments or want to report bugs, please write them in the comments section below.


  • 2010-04-10 - First release to


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


About the Author

Uwe Keim
Chief Technology Officer Zeta Producer Desktop CMS
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

In his free time, he does climbing, running and mountain biking. Recently he became a father of a cute boy.

Some cool, free software from us:

Deutschsprachiges Forum zu Elektromobilität und Tesla  
Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans. Download now!  
Homepage erstellen - Intuitive, very easy to use. Download now!  
Send large Files online for free by Email

Comments and Discussions

QuestionOU Pin
iam_xor6-Apr-15 22:19
Memberiam_xor6-Apr-15 22:19 
AnswerRe: OU Pin
iam_xor6-Apr-15 22:36
Memberiam_xor6-Apr-15 22:36 
GeneralRe: OU Pin
iam_xor17-Dec-15 16:27
Memberiam_xor17-Dec-15 16:27 
QuestionDays left before password expiration and domain password policy Pin
fpsanti26-Oct-10 4:55
Memberfpsanti26-Oct-10 4:55 

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.