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


jQuery Mobile ListView
In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
JQuery Mobile Widgets Overview
An overview of widgets in jQuery Mobile.
Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
jQuery Mobile Pages
Brian Mains explains how to create pages with the jQuery Mobile framework.
Exception Handling and .Net (A practical approach)
Error Handling has always been crucial for an application in a number of ways. It may affect the exe...

You might also be interested in the following related blog posts


MSDN Guidance on ASP.NET MVC vs WebForms and its Impact on my EF + ASP.NET Work read more
Idle Timeouts in RIA Services Authentication read more
Telerik Reporting enters the Silverlight space, adds design-time and performance improvements read more
SharePoint 2010 Workflow read more
Dont Repeat Yourself read more
My History of Visual Studio (Part 2) read more
IIS Search Engine Optimization (SEO) Toolkit Announcing Beta 2 read more
Virtual Lab: Windows Forms Security read more
Integrating OpenID in an ASP.NET MVC Application using DotNetOpenAuth read more
Stimulsoft Reports. New versions of reporting tools for .NET, Web, and WPF 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.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.