5,691,626 members and growing! (13,054 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Active Directory     Intermediate

Wrapper API for using Microsoft Active Directory Services

By Syed Adnan Ahmed

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
C#, VB, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 5 May 2005
Updated: 5 May 2005
Views: 175,648
Bookmarked: 114 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
42 votes for this Article.
Popularity: 6.70 Rating: 4.13 out of 5
5 votes, 11.9%
1
0 votes, 0.0%
2
1 vote, 2.4%
3
9 votes, 21.4%
4
27 votes, 64.3%
5

Overview

Applies To:

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

Summary:

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 developed wrapper API

Contents

  • Introduction
  • What is this article about?
  • How Does Active Directory Work?
  • What’s inside Wrapper API for Active Directory?
  • Platforms Tested
  • Conclusion

Introduction

Active Directory provides the ability to authenticate and authorize the 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 is using 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 purpose. As Active Directory integrated with Windows Operating System, which means very intrinsic support is available at a very low level.

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 Directory Services namespace which enables programmers to access ADSI objects using System.DirectoryServices namespace.

How Does Active Directory Work?

Active Directory is simply a hierarchical, object-orientated 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-administrate 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 users friendly I created a wrapper API in VB.NET and C#.NET, which performs all the operations as developer needs in order to navigate the active directory.

By using wrapper API, 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
  • Group Exist
  • IsUserValid
  • Load All Users
  • Load All Groups
  • Load Group
  • Load User
  • Login
  • Remove User From Group
  • User Exist
  • Set Password
  • Update User
  • Update Group

What’s Inside Wrapper API for Active Directory?

As shown in the figure#1, the wrapper API consists of 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 the user in the particular Active directory group, following code will be used.

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

To check, whether the user exist in the 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

ADGroup class consists of properties and method responsible for dealing with Active directory groups.

I mapped the following properties with 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 the group in the Active Directory.

Below are the codes snipped for creating the 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

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

  1. “FirstName” Mapped With “givenName”
  2. ‘MiddleInitial” Mapped With “initials”
  3. “LastName” Mapped With “sn”
  4. “UserPrincipalName” Mapped With “UserPrincipalName”
  5. “PostalAddress” Mapped With “PostalAddress”
  6. “MailingAddress” Mapped With “MailingAddress”
  7. “ResidentialAddress” Mapped With “HomePostalAddress”
  8. “Title” Mapped With “Title”
  9. “HomePhone” Mapped With “HomePhone”
  10. “OfficePhone” Mapped With “TelephoneNumber”
  11. Mobile” Mapped With “Mobile
  12. “HomePhone” Mapped With “HomePhone”
  13. “Fax” Mapped With “FacsimileTelephoneNumber”
  14. “Email” Mapped With “Email”
  15. “Url” Mapped With “Url”
  16. “UserName” Mapped With “sAMAccountName”
  17. “DistinguishedName” Mapped With “DistinguishedName”
  18. “IsAccountActive” to check the user status in the active directory.

How to Use

1. The ADUser class is used to create the user. The code snipped to create the user is given below:

Dim _AdUser As New ActiveDirectory.ADUser
_AdUser.FirstName = "Syed"
_AdUser.MiddleInitial = "Adnan"
_AdUser.LastName = "Ahmed" '

_AdUser.Email = "adnanahmed235@yahoo.com"
_AdUser.UserName = "adnan"
_AdUser.Password = "123456"
_AdUser.IsAccountActive = True
_AdUser.MailingAddress = "Riyadh, Saudi Arabia"
_AdUser.Title = "Software Engineer"
_AdUser = ADManager.Instance.CreateADUser(_AdUser)

2. If you want to update the user in the Active Directory use the following code snipped.

Dim _AdUser As ADUser
_AdUser = ADManager.Instance.LoadUser("adnan")
_AdUser.MailingAddress = "Jeddah, Saudi Arabia"
_AdUser.Title = "Senior Software Engineer"
_AdUser.Update()

3. You can use ADUser class to reset the user password.

Dim _AdUser As ADUser
_AdUser = ADManager.Instance.LoadUser("adnan")
_AdUser.SetPassword("654321")

Utility Class

Utility class is responsible for general options.

Configuration Changes

Before using the wrapper API, You have to follow the following instructions for windows and web based applications.

Web Based Application

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

<identity impersonate="true" />

Add the following lines of tags inside the <appSettings> tags.

<add key="Domain" value="MyDomain.com" />
<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 ‘ADUsersPath’ Key, value (“OU=DeveloperDepartment,") shows the OU= Organizational Unit in the Active Directory as an example. You can write any of your organizational unit or create new one for testing.

Go to IIS select the website, In the properties windows, select the Directory Service Tab, In the Authentication and access control option, Click Edit Button, It will Open Authentication Methods window, select Anonymous access and enter Domain Administrator Account User Name, Password and select Integrated Windows Authentication as shown in the following figures.

Figure #2

Figure #3

Windows Based Application

Add the following lines of tags inside the <configuration> tags.

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

Note: Sample App.config file is included in the download API.

Platforms Tested

I have tested the included project on following platforms

  • Windows Server 2003
  • Windows XP SP1 or SP2

Conclusion

I have demonstrated, how easy it is to navigate the Active Directory Objects by using the wrapper API which is using System.DirectoryServices. In the next release of my wrapper API, I will demonstrate how to manage Active Directory Roles and Permission 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Syed Adnan Ahmed


Adnan Ahmed is a Senior MS Solutions Consultant, working with all major versions of SharePoint and its various complimentary technologies (Project Server, CMS, and K2) has been both enthused and frustrated by the capabilities for over 6 years now.

Adnan, based in Ireland, works for one of the largest IT Company in Ireland and has worked with many large enterprises to help them realise real benefits from adopting and integrating the SharePoint platform. Microsoft Certified Technology Specialist (MCTS) for SharePoint, MCSD.NET

Email: adnan@projectservergurus.com
Owner: http://www.projectservergurus.com
My Blogs:
Project Server: http://www.projectservergurus.com/adnan
SharePoint:
http://www.sharepointblogs.com/adnan

Occupation: Web Developer
Location: Ireland Ireland

Other popular Hardware & System articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 79 (Total in Forum: 79) (Refresh)FirstPrevNext
Generalmissing valuesmembersalvatore.borrelli1:04 19 Jun '08  
Generallist all groups and usersmembersalvatore.borrelli0:16 19 Jun '08  
QuestionAddUserToGroup error (vb.netmemberizzyfox8:17 4 Apr '08  
Generaljust what im looking formemberGuy Harwood6:33 25 Feb '08  
GeneralSet Password to Never ExpiresmemberBarry S7:31 10 Nov '07  
QuestionHow To Trace changes in ADmembersuriiitm0:02 14 Sep '07  
QuestionHow to delete user and move user to specific OU?memberdnvihc21:12 29 Aug '07  
QuestionAdd user to specific OU?memberdekana2:25 2 Aug '07  
GeneralSecuritymembertp-ayman0:06 24 Jul '07  
GeneralRe: Securitymemberiamtherookie11:29 26 Feb '08  
GeneralAn unhandled exception of type 'System.ArgumentNullException' occurred in activedirectory.dllmembermmansf11:19 9 Jun '07  
Generalerror while run the code....memberkoolprasad20030:58 8 Jun '07  
GeneralNull Reference Error !!!!!!!!!!!membernaimish_hit5:24 22 Sep '06  
GeneralSystem.NullReferenceExceptionmemberTBBASEFLUG6:44 8 Sep '06  
GeneralRe: System.NullReferenceExceptionmember_ME_IS_TINUS4:44 13 Sep '06  
GeneralUseful ArticlememberNoelGDsouza6:50 24 Aug '06  
GeneralWho here thinks this design is a bad idea?memberMike Niccum6:20 13 Jul '06  
GeneralCan I do this?memberHZ_799:04 5 Jul '06  
Generalhow to use this code in my .net projectmemberiamjagadishv2:28 8 Jun '06  
GeneralRe: how to use this code in my .net projectmemberjamesvickers5:12 8 Jun '06  
Question? Can I access LDAP of Solaris environment from my Windows environment.memberSuranjan Nandi4:16 9 Mar '06  
GeneralContribute to Syed (LogonHours)memberSpyram8:24 2 Mar '06