Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
Using Forms Authentication with Active Directory
Forms Authentication is a system in which unauthenticated requests are redirected to a Web form where users are required to provide their credentials.
Upon submitting the form, and being properly verified by your application, an authorization ticket is issued by Web application, in the form of a cookie. This authorization cookie contains the user's credentials or a key for reacquiring the user's identity (therefore making the user's identity persistent). In essence, Forms Authentication is a means for wrapping your Web application with a thin security layer, allowing you to have your own custom login interface and verification functionality.
Active Directory is an essential and inseparable element of the Windows 2000 network architecture that lets organizations efficiently share and manage information about network resources and users. It's essentially a single point of management for Windows-based user accounts, clients, and applications. It also helps organizations integrate non-Windows application with Windows-based applications and devices, thus consolidating directories and easing management of the entire network operating system. Organizations also use Active Directory to extend systems securely to the Internet by forcing their Web application users to authenticate them against their single-point Active Directory.
Microsoft Windows® 2000
In this article I'll show how to implement form authentication using Active Directory warehouse credentials. The ASP.NET forms authentication allows users write their credentials (user name and password) in a web form to identify themselves. The Web application receives the credential, and it can authenticate the user verifying his user name and password in a data set available. This article describes how to authenticate users in Microsoft® Active Directory® directory service using the protocol LDAP (Light Data Access Protocol). It also describes how to store that information in a GenericPrincipal object and how to store it in HttpContext.Current.User property that follows the request throw the ASP.NET Web application.
Creating the Login Page
The first thing we have to do is to create a new solution in Visual Studio 2005. And add a new web site (if you like, you can download sample User Authentication with Active Directory Visual Studio 2005 project, used in this tutorial). The website must have a simple login page, like the one I show in the next image. The objective of this page is like every login page, to validate the username and password in a domain, the difference is that the validation process will validate against Active Directory.
On the page load event of the login page you can add the following code to display the identity domain and username associated with the current Web request.
Configuring the application
Now we have to configure the application in order that admits the authentication by Forms. You can do this trough the web.config file. You have to change the authentication mode in the web.config and add a secondary element to set the login page, the session timeout and the cookie that contains the authentication info.
Also, you must add the <authorization> tag to allow only the authenticated users have access to the application. If the login process was not successful the user will be redirected to the login page.
For that go to the application's virtual directory Properties, then on the security tab click on Anonymous access and authentication control group and clear the Anonymous access check box.
After that a little modification in the web.config is needed. You will have to add an <identity> element beneath the <authorization> element in Web.config and set the impersonate attribute to true. This causes ASP.NET to impersonate the anonymous account specified earlier.
So your configuration file should looks like this one:
As result of this configuration, every request directed to the application will be executed in the configured anonymous account security context. The user will provide credentials through the Web form to authenticate itself in Active Directory, but the account that will be used to have access to Active Directory will be the configured anonymous account.
Implementing the Active directory validation
Now it is time to implement the active directory authentication code. You will create a new class that will provide a method that receives a domain name, user name and password as parameter and returns a Boolean value indicating if there is a matching record in Active Directory.
Yow could start creating a class library with a class that implements an authentication method against AD. You will have to add a new assembly reference to System.DirectoryServices.dll. This provides access to the namespace System.DirectoryServices which contains administered types as it helps in the consultation and treatment in Activate Directory.
The connection to active directory is through LDAP. You have to specify the connection path, it is highly recommended to store this path in a configuration file so the application's administrator could change anytime he wants. For the searching process AD will need a filter attribute.
Initially, the method tries to connect with Active Directory using the provided credentials. If it is successful, the method uses the administered class DirectorySearcher to search the specified user object. If exists, the path is updated so that it aims at the user object, and the filter attribute is updated with the user object's common name attribute.
Your class should looks like the next one
Implementing the authentication process
In this step you are going to develop the fully authentication process that will be launched when the user clicks on the login button using the class you build up before.
For the authenticated users, a form authentication credential will be created and the user will be redirected to the original page that he asked for.
You should add on the login button event handler some code to create a new active directory validation class instance so that it aims at Active Directory in LDAP server. The authentication process will do the following:
a. Authenticate the user against Activates Directory.
to you LDAP directory server.
Remember that you must change the access path so that aims at the AD Server, and the cookie's name must be the same name that you specified before in the web.config file.
Implementing an authentication request controller
Now the question is how the application to knows if the user is authenticated or not every time the application makes a request.
You can do this procedure in the global.asax file. You have to implement an event handler named Application_AuthenticateRequest on it. Remember that in previous steps we generate a cookie whit an authentication ticket on it. In this event handler you must extract that cookie information and create a GenericPrincipal object identifying the authenticated user.
Finally, the GenericPrincipal object will be associated with the current HttpContext object created for each web request.
Testing the application
We already finished the authentication process and it is time to test it. Enter a valid domain name, user name, and password and then click Log On, if you are successfully authenticated, you should be redirected back to the page you set as default. If you try to change the url manually before login, you will get the same result as a invalid login, the application will note that there is no authenticated user so it will be redirected to login page again.
I suggest you to debug the application so you can understand how it is working.
comments powered by Disqus