惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
Vercel News
Vercel News
F
Fortinet All Blogs
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
小众软件
小众软件
月光博客
月光博客
A
About on SuperTechFans

博客园 - 执法长老

软件开发模型 Enterprise Library 3.1中文文档 深入 Unity 1.x 依赖注入容器之四:依赖注入 深入 Unity 1.x 依赖注入容器之三:获取对象 深入 Unity 1.x 依赖注入容器之二:初始化 Unity Microsoft .NET Pet Shop 4 架构与技术分析 深入 Unity 1.x 依赖注入容器之一:入门 Unity 1.0 中文文档:1 - Unity 简介 二、ASP.NET Runtime Pipeline(续ASP.NET Http Runtime Pipeline - Part I)(转) ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline - Part I(转) ASP.NET Process Model之一:IIS 和 ASP.NET ISAPI(转) 深入剖析ASP.NET的编译原理之二:预编译(Precompilation)(转) 深入剖析ASP.NET的编译原理之一:动态编译(Dynamical Compilation)(转) 使用 Unity (三):理解和使用依赖注入的键 使用 Unity(二):配置 Unity 、读取配置信息和获取对象 使用PIAB和VAB实现业务实体的自动验证 使用 Unity(一):Unity 应用程序块容器介绍 SQL Server性能优化的一些简单技巧 Unity&Webform(2):自定义LifetimeManager和TypeConverter使Unity从HttpContext中取值注入WebForm页面 (转)
Wrapper API for using Microsoft Active Directory Services
执法长老 · 2008-12-19 · via 博客园 - 执法长老

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.

Collapse

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.

Collapse

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.

Collapse

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:

Collapse

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.

Collapse

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.

Collapse

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.

Collapse

<identity impersonate="true" />

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

Collapse

<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.

Collapse

<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.