Click here to Skip to main content
Click here to Skip to main content

Active Directory Helper

, 10 Apr 2010
Rate this:
Please Sign up or sign in to vote.
A small set of classes for simplifying the access to Windows Active Directory

Introduction

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.

Background

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)
    {
        Trace.WriteLine(group.Name);
    }
}

if (allUsers != null)
{
    foreach (var user in allUsers)
    {
        Trace.WriteLine(user.Name);
    }
}

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

Epilog

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.

History

  • 2010-04-10 - First release to CodeProject.com

License

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:
 
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
Offline-Homepage-Baukasten

Comments and Discussions

 
QuestionDays left before password expiration and domain password policy Pinmemberfpsanti26-Oct-10 3:55 
Hello!
 
First of all thank you for your very useful library.
 
I would like to ask you if it is possible to get the domain password policy and the days left before password expiration.
 
Thank you in advance,
Francesco

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 10 Apr 2010
Article Copyright 2010 by Uwe Keim
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid