Published: 06 Jul 2009
By: Akhtar Shiekh

Learn how to authenticate a Windows account using Forms Authentication.

Contents [hide]

Introduction

Last month I worked on a small assignment to authenticate a Windows account (Domain or Local) using Form authentication. The purpose of this task was to facilitate our application users to login with any valid Windows account (Instead of automatically authentication of windows logged in user).As it was an interesting task, I decided to share my experience with you

Requirements

The application should authenticate Windows users using Form authentication, so that the currently logged in user shouldn’t be bound to login in the application only with his Windows account. He should be able to login with any valid Windows account.

Solution

We need to do the following steps to get the desired functionality:

  • Configure Authorization and Authentication settings in web.config.
  • Create a login page and execute logic to authenticate provided credential of Windows user.
  • If provided credentials are authenticated in step 2, generate an authentication token so that user should be able to navigate into the authorized pages of your application.

Let’s talk about each step,

Configure Authorization and Authentication settings in web.config.

We need to use Form authentication. The user will enter his Windows credentials in the form and we will validate them by using custom logic in step 2.

To restrict anonymous access, you need the following authorization settings in web.config

Create a login page and execute logic to authenticate the provided credentials of Windows user

We need to create a login page (e.g. Login.aspx) to get username and password information from the user and then validate them.

We have different options to validate Windows credentials. The way I choose is the LogonUser() method of a Win32 API called Advapi32.dll.

The LogonUser function attempts to log a user on to the local computer. This method takes a username, password and other information as input and returns a Boolean value to indicate if the user is logged in or not. If it returns true, this means the provided username and password are correct.

To use this method in our class, we need to include the following namespace:

Then, the add method declaration with the DLLImport attribute (as this is a method of an unmanaged Win32 DLL).

According to the MSDN Documentation:

lpszUsername [in]: A pointer to a null-terminated string that specifies the name of the user. This is the name of the user account to log on to. If you use the user principal name (UPN) format, User@DNSDomainName, the lpszDomain parameter must be NULL.

lpszDomain [in, optional]: A pointer to a null-terminated string that specifies the name of the domain or server whose account database contains the lpszUsername account. If this parameter is NULL, the user name must be specified in UPN format. If this parameter is ".", the function validates the account by using only the local account database.

lpszPassword [in]: A pointer to a null-terminated string that specifies the plaintext password for the user account specified by lpszUsername. When you have finished using the password, clear the password from memory by calling the SecureZeroMemory function. For more information about protecting passwords, see Handling Passwords.

dwLogonType [in]: The type of logon operation to perform.

dwLogonProvider [in]: Specifies the logon provider.

phToken [out]: A pointer to a handle variable that receives a handle to a token that represents the specified user.

Generate an authentication token, if the provided credentials are authenticated

If the provided credentials are authenticated by the LogonUser() method then we need to generate an authentication token so that the user should be able to navigate into the authorized pages of the application. FormsAuthentication.RedirectFromLoginPage() or FormsAuthentication.SetAuthCookie() can be used for this purpose.

Here is the login button’s Click handler code for authentication and generation of the authentication token. Comments will help you to understand the code.

Let’s put it all together:

Listing 1: Login.aspx

Listing 2: Login.aspx.cs

Summary

You’ve learned how to authenticate a Windows user using forms authentication. You used the Win32 API method LogonUser() to authenticate a Windows user and generate a form authentication ticket for that user so that he can navigate your website.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Akhtar Shiekh

I am Microsoft Certified Technology Specialist for Web Application Development. I have 4 year experience of Web and Distributed application development.I have considerable experience developing client / server software for major corporate clients using the Windows operating systems and .NET platform...

This author has published 2 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


ASP.NET MVC Dynamic Model Binding
Dynamic model binding enables a single action to handle multiple forms.
Implementing Website Pinning in ASP.NET
Learn to enable website pinning features of IE9 on Windows 7 for your ASP.NET websites.
Building a YouTube-like Video Playing Site Using ASP.NET Techniques - Part 3
In this final part of this series, we are going to discuss how to make the users play their selected...
Using HTML5 Web Storage in ASP.NET
HTML5 Web Storage provides a streamlined data storage mechanism that allows developers to store data...
Running multiple websites in a single hosting space
Develop a solution that supports multiple websites in a single hosting space and database.

You might also be interested in the following related blog posts


Export Word documents to XPS - Open XML Paper Specification format read more
Idle Timeouts in RIA Services Authentication read more
Stimulsoft Reports. New versions of reporting tools for .NET, Web, and WPF read more
Some Words on the new RadForm and RadRibbonForm read more
Demeter Transmogrifiers To The Rescue read more
The Law of Demeter Is Not A Dot Counting Exercise read more
Reporting Q2 2009 release read more
Telerik Reporting enters the Silverlight space, adds design-time and performance improvements read more
Running development from a RAM disk options and products read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 3: Authentication read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Good Article Param Iyer 7/23/2009 5:57 AM
RE: Good Article Akhtar Shiekh 7/23/2009 6:22 AM
placeholder Thank you very much Mallesh Rao 8/18/2009 5:35 AM
Good one Venkat Bhogavilli 8/18/2009 9:12 AM
placeholder Thank you King Alex 8/31/2009 6:52 AM
Excellent article! Roger Moore 12/1/2009 7:02 PM

Please login to rate or to leave a comment.