This article deals with the management of User Login and Password information for Forms Authentication in ASP.Net. The article shows how to update existing users password and add new users when the ASP.Net application is configured to allow for passwords hashed using SHA1 format. The article can be easily modified to use the MD5 format or clear format for passwords.
As a .Net programmer, you must have definitely come across the Forms Authentication for securing ASP.Net applications as an alternative to Windows Authentication. In this sample we create a sample web site which is secured using Forms Authentication. We store our user names and passwords in the web.config file. We will create a web page for administering the user security information. We will allow access to only the Admin user to modify the user security information.
Step 1 : Create the Web Application
In this article, I will describe the steps assuming use of Visual Studio.Net. Code Listings are included at the end of the article for users who prefer other code editors.
Step 2 : Modify the Web.Config File
Modify the Web.Config file to specify the authentication mode of "Forms". Replace the authentication section to include the following xml snippet:
<
<forms loginUrl="login.aspx" name=".ASPXCOOKIEAUTH" path="/">
<credentials passwordFormat="SHA1">
<user name="User1" password="4A60935FE851C99148EFD66122CDD0F43D5A3059" />
<user name="User2" password="114B3C899D75F5A3FE3FDF83531C42E33214555B" />
<user name="Admin" password="1F95EC61B6EF02B5D2B138654DA138BFDBBC7F3C" />
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
XML Snippet: Configure our application for Forms Authentication
Step 3: Design the Login Form
Now we move on to design the Login.aspx form. This form is specified in our web.config file as the form which will be presented to the user, if there is no authentication in the requested form. It is a good practice to setup this form to use SSL.
The login form consists of 2 text boxes for accepting the user name and password input from the user and a Login button.

Figure 1: Layout of the Login.aspx form
| Control | Property Name | Property Value |
| Label | Text | Login |
| TextBox | ID | txtLogin |
| Label | Text | Password |
| TextBox | ID | txtPassword |
| TextMode | Password | |
| Button | ID | btnSubmit |
| Text | Submit |
Table : Control Properties for Login.aspx Layout
Add the following statement to the top of this web form.
using System.Web.Security;
Add the code for the Submit button's Click event handler. This will handle the login action.
private void Button1_Click(object sender, System.EventArgs e)
{
if (FormsAuthentication.Authenticate(TextBox1.Text, TextBox2.Text))
{
FormsAuthentication.SetAuthCookie(TextBox1.Text, true);
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, true);
}
}
Code Snippet : Do the Login.
Step 4: Create the application.
Now we can proceed to creating the various pages and other elements of the Web Forms application. Note that the security settings permeate to the sub-folders, unless you specify a separate web.config file for the sub-folder.
We will keep our web application very simple - it will consist of only a default page which will display a greeting to the user and offer an option for the Admin functionality.
(I renamed the page WebForm1.aspx which is created as the default web page in the web application to default.aspx)
Add the following statement to the top of this web form.
using System.Web.Security;
| Control | Property Name | Property Value |
| Label | ||
| LinkButton | Text | Go to Security Admin Web Page |
| NavigateURL | Admin/Admin.aspx | |
| Button | Text | Logout |
| ID | btnLogout |
Table : Properties for controls on default.aspx
Code for Displaying a Greeting to the user :
Add the following code snippet to the Page_Load event of the Web Form.
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = "Hello " + Context.User.Identity.Name;
}
Code Snippet : Greet the user
Code for the Logout Button :
Add the following code to signout from the web application in the Click event of the Logout button
private void tmpLogout_Click(object sender, System.EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect("login.aspx");
}
Code Snippet : Logout from the application
Step 5 : Test the Application
Let's take a break and see how the application works so far.
To test the logins, here are the values specified in the config file
| User Name | Password |
| User1 | 1resU |
| User2 | 2resU |
| Admin | AdminAdmin |
Join the conversation! Your thoughts help the community grow.