Sunday, May 23, 2010

Pre-populating the Login Control’s Username and Password

We’ve all visited websites that provide a test or demo site and allow you to login for the purpose of testing features out using specific user accounts with assigned roles. If you have a website or web application that needs to provide this type of functionality, I’m going to show you how you can save the user a few keystrokes by filling in the Username and Password for them, so that all they have to do is click the Login button.

What I have done is created three accounts: Admin, User, and Guest along with a Default.aspx page which resides under the respective folder. The folder structure is as follows for these accounts:

Admin/Default.aspx

User/Default.aspx

Guest/Default.aspx

I’ve created a login page (Login.aspx) and dropped a Login Control on it. The Login Control has been converted to a template and I’ve added a label and dropdown list to it. The dropdown list contains the user accounts that will be selected for logging in.

image

Within the Load event of the Login Control I get a reference to the dropdown list and password textbox controls, so that they can be used in selecting the proper user and populating the password textbox. I add an attribute to the password textbox, which sets the password, so that the user does not have to type it in. Something to be mindful of is that the password is viewable in plain text if you view the pages source. Because these accounts are being used to save the user a few of keystrokes for a test or demo site login this isn’t much of an issue.

DropDownList userLogin = (DropDownList)DemoLogin.FindControl("UserDropDownList");
TextBox passwd = (TextBox)DemoLogin.FindControl("Password");
switch (userLogin.SelectedValue)
{
   case "Admin":
       DemoLogin.UserName = userLogin.SelectedValue;
       passwd.Attributes.Add("value", "Adm!n123");
       break;
   case "User":
       DemoLogin.UserName = userLogin.SelectedValue;
       passwd.Attributes.Add("value", "U$er123");
       break;
   case "Guest":
       DemoLogin.UserName = userLogin.SelectedValue;
       passwd.Attributes.Add("value", "Gue$t123");
       break;
   default:
       DemoLogin.UserName = string.Empty;
       passwd.Attributes.Add("value", "");
       break;
}


The only other thing needed is within the Login Control’s LoggedIn event to check the role of the user and then redirect them to the appropriate Default.aspx page.



if (Roles.IsUserInRole(DemoLogin.UserName, "Admin"))
{
   Response.Redirect("~/Admin/Default.aspx");
}
else if (Roles.IsUserInRole(DemoLogin.UserName, "User"))
{
   Response.Redirect("~/User/Default.aspx");
}
else if (Roles.IsUserInRole(DemoLogin.UserName, "Guest"))
{
   Response.Redirect("~/Guest/Default.aspx");
}
else
{
   Response.Redirect("~/Login.aspx");
}


image 



While this is pretty trivial, it does save a few keystrokes by allowing the user to just select the user account they wish to login as and then all they have to do is click the login button.



PopulateLoginSample.zip

No comments: