Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#

Active Directory Helper

Rate me:
Please Sign up or sign in to vote.
4.88/5 (10 votes)
10 Apr 2010CPOL2 min read 55.7K   4.6K   55   4
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:

C#
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:

C#
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:

C#
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)


Written By
Chief Technology Officer Zeta Software GmbH
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.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions

 
QuestionOU Pin
iam_xor6-Apr-15 21:19
iam_xor6-Apr-15 21:19 
How to get OU of user with your lib?

AnswerRe: OU Pin
iam_xor6-Apr-15 21:36
iam_xor6-Apr-15 21:36 
GeneralRe: OU Pin
iam_xor17-Dec-15 15:27
iam_xor17-Dec-15 15:27 
QuestionDays left before password expiration and domain password policy Pin
fpsanti26-Oct-10 3:55
fpsanti26-Oct-10 3: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.