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.
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.
|
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
|
|
Please login to rate or to leave a comment.