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