Click here to Skip to main content
13,201,452 members (77,072 online)
Click here to Skip to main content
Add your own
alternative version


162 bookmarked
Posted 5 May 2005

Wrapper API for using Microsoft Active Directory Services

, 5 May 2005
Rate this:
Please Sign up or sign in to vote.
If you develop web applications with Microsoft® ASP.NET and have the need to secure your site from unauthorized access, you have surely investigated the various authentication and authorization techniques that ASP.NET 1.x enables. This article discusses how to use Microsoft Active Directory Services


Applies To

  • Microsoft® ASP.NET 1.x
  • Microsoft® Visual Studio® .NET 2003
  • Microsoft® Active Directory® Services


If you are developing web applications utilizing Microsoft® ASP.NET and have the need to secure your site from unauthorized access, you have surely investigated the various authentication and authorization techniques that ASP.NET 1.x enables. This article discusses how to use Microsoft Active Directory Services by using the developed wrapper API.



Active Directory provides the ability to authenticate and authorize users from a centralized location, so users don't need to remember the password for every application, if they use Active Directory for authentication. Microsoft uses Active Directory in almost all of their application servers like Microsoft Content Management Server, Microsoft Share Point Portal Server, Microsoft CRM, and Microsoft Exchange Server etc., for centralized authentication and authorization purposes. As Active Directory is integrated with the Windows Operating System, very intrinsic support is available at a very low level.

The Active Directory Services Interface (ADSI) has always been a very effective way of dealing with users in a Windows network. The System.DirectoryServices namespace gives users access to some rudimentary user administration via ASP.NET. ADSI classes in the DirectoryServices namespace enables programmers to access ADSI objects using the System.DirectoryServices namespace.

How does Active Directory work?

Active Directory is simply a hierarchical, object-oriented database that represents all of your network resources. At the top, there's typically the Organization (O), beneath that Organizational Units (OU) as containers, and finally, objects that consist of your actual resources. This hierarchical format creates a very familiar and easy-to-administer tree for systems administrators. For example, if you assign an OU access to a given resource, that access will also be persisted to the objects that are contained within it.

What is this article about?

Active Directory Services is a bit complex, so to make it more user friendly, I created a wrapper API in VB.NET and C# .NET, which performs all the operations a developer needs in order to navigate the Active Directory.

By using the wrapper API, the developer can do the following operations:

  • Add User to Group
  • Create Active Directory Group
  • Create Active Directory User
  • Delete Active Directory Group Account
  • Delete Active Directory User Account
  • Enable Active Directory User Account
  • Does Group Exist
  • Is User Valid
  • Load All Users
  • Load All Groups
  • Load Group
  • Load User
  • Login
  • Remove User From Group
  • Does User Exist
  • Set Password
  • Update User
  • Update Group

What's inside the wrapper API for Active Directory?

As shown in the Figure 1, the wrapper API consists of the following classes:

ADManager class

ADManager is a singleton class responsible for managing the users and groups in the Active Directory.

How to use

To add a user to a particular Active Directory group, the following code will be used:

Dim _ADUser As ADUser
_ADUser = ADManager.Instance.LoadUser("adnan")
Dim _ADGroup As ADGroup
_ADGroup = ADManager.Instance.LoadGroup("DeveloperGroup")

To check whether a user exists in Active Directory, the following simple code will be used:

If ADManager.Instance.UserExists("adnan") Then
    MsgBox("User Exist in the Active Directory")
End If

ADGroup class

The ADGroup class consists of properties and methods responsible for dealing with Active Directory groups.

I map the following properties with an Active Directory Group in order to make the properties simple.

  • "Name" mapped with "cn"
  • "DisplayName" mapped with "DisplayName"
  • "DistinguishedName" mapped with "DistinguishedName"
  • "Description" mapped with "Description"

How to use

The ADGroup class is used to create/update groups in the Active Directory. Shown below is a code snippet for creating a group in Active Directory:

Dim _AdGroup As New ADGroup
_AdGroup.Name = "DeveloperGroup"
_AdGroup.Description ="All developers in the company"
_AdGroup = ADManager.Instance.CreateADGroup(_AdGroup) 

ADUser class

The ADUser class consists of properties and methods responsible for dealing with Active Directory users. The ADUser properties and the corresponding property in the Active Directory are given below:

  • "FirstName" mapped with "givenName"
  • "MiddleInitial" mapped with "initials"
  • "LastName" mapped with "sn"
  • "UserPrincipalName" mapped with "UserPrincipalName"
  • "PostalAddress" mapped with "PostalAddress"
  • "MailingAddress" mapped with "MailingAddress"
  • "ResidentialAddress" mapped with "HomePostalAddress"
  • "Title" mapped with "Title"
  • "HomePhone" mapped with "HomePhone"
  • "OfficePhone" mapped with "TelephoneNumber"
  • "Mobile" mapped with "Mobile"
  • "HomePhone" mapped with "HomePhone"
  • "Fax" mapped with "FacsimileTelephoneNumber"
  • "Email" mapped with "Email"
  • "Url" mapped with "Url"
  • "UserName" mapped with "sAMAccountName"
  • "DistinguishedName" mapped with "DistinguishedName"
  • "IsAccountActive" to check the user status in the Active Directory

How to use

  1. The ADUser class is used to create a user. The code snippet to create the user is given below:
  2. Dim _AdUser As New ActiveDirectory.ADUser
    _AdUser.FirstName = "Syed"
    _AdUser.MiddleInitial = "Adnan"
    _AdUser.LastName = "Ahmed" '
    _AdUser.Email = ""
    _AdUser.UserName = "adnan"
    _AdUser.Password = "123456"
    _AdUser.IsAccountActive = True
    _AdUser.MailingAddress = "Riyadh, Saudi Arabia"
    _AdUser.Title = "Software Engineer"
    _AdUser = ADManager.Instance.CreateADUser(_AdUser)
  3. If you want to update the user in the Active Directory, use the following code snippet:
  4. Dim _AdUser As ADUser
    _AdUser = ADManager.Instance.LoadUser("adnan")
    _AdUser.MailingAddress = "Jeddah, Saudi Arabia"
    _AdUser.Title = "Senior Software Engineer"
  5. You can use the ADUser class to reset the user password.
  6. Dim _AdUser As ADUser
    _AdUser = ADManager.Instance.LoadUser("adnan")

Utility class

The Utility class is responsible for the general options.

Configuration changes

Before using the wrapper API, you have to follow the instructions below for Windows and web based applications.

Web based applications

Add the following line inside the <system.web> tag in the web.config file:

<identity impersonate="true" />

Add the following lines inside the <appSettings> tags:

<add key="Domain" value="" />
<add key="ADPAth" value="LDAP://MyDomain " />
<add key="ADUser" value="administrator" />
<add key="ADPassword" value="123" />
<add key="ADUsersPath" value="OU=DeveloperDepartment," />

Note: Here in the 'ADUsersPath' key, the value ("OU=DeveloperDepartment,") shows the OU= Organizational Unit in the Active Directory as an example. You can write any of your organizational units or create a new one for testing.

Go to IIS, select the website, in the Properties window, select the Directory Service tab, in Authentication and Access Control Options, click the Edit button. It will open the Authentication Methods window, select Anonymous Access and enter the Domain Administrator Account user name and password, and select Integrated Windows Authentication, as shown in the following figures:

Figure #2

Figure #3

Windows based applications

Add the following lines inside the <configuration> tags:

<add key="Domain" value="" />
<add key="ADPAth" value="LDAP://MyDomain " />
<add key="ADUser" value="administrator" />
<add key="ADPassword" value="123" />
<add key="ADUsersPath" value="OU=DeveloperDepartment," />

Note: A sample App.config file is included in the download file.

Platforms tested

I have tested the project on the following platforms:

  • Windows Server 2003
  • Windows XP SP1 or SP2


I have demonstrated how easy it is to navigate Active Directory objects by using the wrapper API which uses System.DirectoryServices. In the next release of my wrapper API, I will demonstrate how to manage Active Directory Roles and Permissions by using the wrapper API. I have given the API in both VB.NET and C#.NET, and you can use it in both Windows and web based applications.


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


About the Author

Syed Adnan Ahmed
Architect Version 1
Ireland Ireland
Adnan Ahmed is SharePoint Architect in Version 1(, the IT Consulting Company in Ireland and has involved with many large enterprises to help them realise real benefits of SharePoint 2007|2010.

SharePoint Architect | Blogger | IT Evangelist | MCPD SharePoint 2010 Developer| MCITP SharePoint Administrator 2010

Linked In Profile:

My Blogs:

You may also be interested in...


Comments and Discussions

Questionplease help me Pin
Member 1222298228-Dec-15 6:41
memberMember 1222298228-Dec-15 6:41 
QuestionTesting it in a C# Windows Program Pin
Dragon1313us16-Mar-12 23:13
memberDragon1313us16-Mar-12 23:13 
GeneralPlease help me... Pin
raky2824-Aug-09 2:32
memberraky2824-Aug-09 2:32 
please help me. we are unable to create a use account, but we are able to delete an account, and also display a list of 1000 users, despite the fact that there are more than 1000 users(there nearly 2000 users in our active directory), why this limit?

and please why cant we create a user account?

we are using the following statement :

ad = ActiveDirectory.ADManager.Instance.CreateADUser(ad);
GeneralError while removing User from a group Pin
N Surekha27-Apr-09 11:38
memberN Surekha27-Apr-09 11:38 
Generalmissing values Pin
salvatore.borrelli19-Jun-08 0:04
membersalvatore.borrelli19-Jun-08 0:04 
Generallist all groups and users Pin
salvatore.borrelli18-Jun-08 23:16
membersalvatore.borrelli18-Jun-08 23:16 
QuestionAddUserToGroup error ( Pin
izzyfox4-Apr-08 7:17
memberizzyfox4-Apr-08 7:17 
AnswerRe: AddUserToGroup error ( Pin
troybooth9-Mar-12 5:56
membertroybooth9-Mar-12 5:56 
Generaljust what im looking for Pin
Guy Harwood25-Feb-08 5:33
memberGuy Harwood25-Feb-08 5:33 
GeneralSet Password to Never Expires Pin
Barry S10-Nov-07 6:31
memberBarry S10-Nov-07 6:31 
QuestionHow To Trace changes in AD Pin
suriiitm13-Sep-07 23:02
membersuriiitm13-Sep-07 23:02 
QuestionHow to delete user and move user to specific OU? Pin
dnvihc29-Aug-07 20:12
memberdnvihc29-Aug-07 20:12 
QuestionAdd user to specific OU? Pin
dekana2-Aug-07 1:25
memberdekana2-Aug-07 1:25 
GeneralSecurity Pin
tp-ayman23-Jul-07 23:06
membertp-ayman23-Jul-07 23:06 
GeneralRe: Security Pin
iamtherookie26-Feb-08 10:29
memberiamtherookie26-Feb-08 10:29 
GeneralAn unhandled exception of type 'System.ArgumentNullException' occurred in activedirectory.dll Pin
mmansf9-Jun-07 10:19
membermmansf9-Jun-07 10:19 
Generalerror while run the code.... Pin
koolprasad20037-Jun-07 23:58
memberkoolprasad20037-Jun-07 23:58 
GeneralNull Reference Error !!!!!!!!!!! Pin
naimish_hit22-Sep-06 4:24
membernaimish_hit22-Sep-06 4:24 
GeneralSystem.NullReferenceException Pin
TBBASEFLUG8-Sep-06 5:44
memberTBBASEFLUG8-Sep-06 5:44 
GeneralRe: System.NullReferenceException Pin
_ME_IS_TINUS13-Sep-06 3:44
member_ME_IS_TINUS13-Sep-06 3:44 
GeneralUseful Article Pin
NoelGDsouza24-Aug-06 5:50
memberNoelGDsouza24-Aug-06 5:50 
QuestionWho here thinks this design is a bad idea? Pin
Mike Niccum13-Jul-06 5:20
memberMike Niccum13-Jul-06 5:20 
AnswerRe: Who here thinks this design is a bad idea? Pin
troybooth17-Mar-11 2:52
membertroybooth17-Mar-11 2:52 
QuestionCan I do this? Pin
HZ_795-Jul-06 8:04
memberHZ_795-Jul-06 8:04 
Questionhow to use this code in my .net project Pin
iamjagadishv8-Jun-06 1:28
memberiamjagadishv8-Jun-06 1:28 
AnswerRe: how to use this code in my .net project Pin
jamesvickers8-Jun-06 4:12
memberjamesvickers8-Jun-06 4:12 
Question? Can I access LDAP of Solaris environment from my Windows environment. Pin
Suranjan Nandi9-Mar-06 3:16
memberSuranjan Nandi9-Mar-06 3:16 
GeneralContribute to Syed (LogonHours) Pin
Spyram2-Mar-06 7:24
memberSpyram2-Mar-06 7:24 
GeneralError in GetUser (interop) Pin
flanderp6-Jan-06 4:01
memberflanderp6-Jan-06 4:01 
GeneralRe: Error in GetUser (interop) Pin
fuhan2-May-06 22:28
memberfuhan2-May-06 22:28 
QuestionCannot use the wrapper API. Pin
Orris16-Dec-05 5:17
memberOrris16-Dec-05 5:17 
GeneralSetPasswor error Pin
fresht13-Dec-05 21:04
memberfresht13-Dec-05 21:04 
GeneralAddUser Error - SetPassword Pin
Luciana Perez7-Dec-05 8:19
memberLuciana Perez7-Dec-05 8:19 
GeneralRe: AddUser Error - SetPassword Pin
davenaylor200029-Mar-06 1:19
memberdavenaylor200029-Mar-06 1:19 
GeneralRe: AddUser Error - SetPassword Pin
kbomb98729-Mar-06 10:08
memberkbomb98729-Mar-06 10:08 
GeneralRe: AddUser Error - SetPassword Pin
kaipira14-Jan-10 7:45
memberkaipira14-Jan-10 7:45 
QuestionHow to enable / disable a computer account? Pin
netyale2-Dec-05 11:05
membernetyale2-Dec-05 11:05 
GeneralCreate Exchange Mailbox Pin
renek200328-Nov-05 5:59
memberrenek200328-Nov-05 5:59 
Generalbug on ADManager.Login() Pin
Yaniv Karta30-Oct-05 23:10
memberYaniv Karta30-Oct-05 23:10 
QuestionCompany property for the user Pin
Nagalingam28-Oct-05 9:42
memberNagalingam28-Oct-05 9:42 
GeneralAdd new user to existing group error's Pin
hellow-world26-Oct-05 22:27
memberhellow-world26-Oct-05 22:27 
GeneralRe: Add new user to existing group error's Pin
kunal197928-Jun-07 13:37
memberkunal197928-Jun-07 13:37 
GeneralexamplePlease Pin
jamesVazquez25-Oct-05 5:36
memberjamesVazquez25-Oct-05 5:36 
GeneralexamplePlease !!!! Pin
jamesVazquez25-Oct-05 5:35
memberjamesVazquez25-Oct-05 5:35 
GeneralJamesVazquez Pin
jamesVazquez25-Oct-05 5:34
memberjamesVazquez25-Oct-05 5:34 
GeneralAduser.groups Pin
JamesVazquez24-Oct-05 11:07
memberJamesVazquez24-Oct-05 11:07 
GeneralActiveDirectory.Utility exception Pin
Ilhamig9-Oct-05 23:55
memberIlhamig9-Oct-05 23:55 
Questionmultiple domain question Pin
the other Ryan27-Sep-05 3:53
memberthe other Ryan27-Sep-05 3:53 
QuestionProblem with Createmailbox Pin
Poseist26-Aug-05 4:27
sussPoseist26-Aug-05 4:27 
AnswerRe: Problem with Createmailbox Pin
kbomb98729-Mar-06 8:13
memberkbomb98729-Mar-06 8:13 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 6 May 2005
Article Copyright 2005 by Syed Adnan Ahmed
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid