October 18, 2006

Exploring Login Control in VS2005:

As the name implies login control provides GUI to log into a website. As we develop websites login feature is quite common functionality in each and every website. I have developed more than 10 web applications and all of them has login feature as common. So In VS2005 Microsoft made the job of the developer easy by introducing Login Controls. This reduces coding as well as development cost.

It comes with two labels, two textboxes one for entering userId and the other for entering password , along with a login button. We can customize the control using auto format . Instruction text and Title text are there to write specific notes that may act as guide to the user. CreateUserText , HelpPageText, PasswordRecoveryText are some of the properties of the login Control that are used to specify text for creating new user, HelpText and the text for password recovery respectively. We can even change the username and password labels. If we enable RememberMeSet property to true then remember me check box highlights .By default it’s true.

VIsibleWhenLoggedIn set to true, the control will be hidden on the page if the user is already authenticated. This option very much useful in designing live websites that provide personalized data when the user is logged in to the website.

Create a new VS2005 Website



Add a login control from login tab in VS2005 toolbar.
I have just given it’s user text for PasswordrecoveryText and CreateUserText and given the corresponding urls in links Section.
Now Double click on the control
You will be redirected to Authenticate event of Login control in code behind .

Now we need to write code for authenticating a user
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = ValidateLogIn(Login1.UserName, Login1.Password);
if (Authenticated == true)
{
Response.Redirect("Home.aspx");
}

}

private bool ValidateLogIn(string UserName, string Password)
{
bool blnRetVal = false;
string strConnection = "data source=HIMABINDU;database=Test;uid=sa;pwd=;";
SqlConnection Connection = new SqlConnection(strConnection);
String strSQL = "Select UserId,Password From UserMaster where UserId =+”’UserName’” + and Password =+”’Password’”;
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader dr;
Connection.Open();
dr = command.ExecuteReader();
while (dr.Read())
{
if ((UserName == dr[0].ToString()) & (Password == dr[1].ToString()))
{
blnRetVal = true;
}

}
dr.Close();
return blnRetVal;

}

The above code is self explanatory as
We need to check Userid and Password from database and if it’s matches correctly then Validate Login returns true .Otherwise it returns false.
It accepts userid and password fields that we need to pass dynamically as Login1.UserId, Login1.Password that’s it.
If the user is present in Database it returns true else it returns false.
This article described usage of login control and authenticating user.

4 comments:

sujatha said...

That was a good one. Really helped me as I am a beginner in .Net.

Anonymous said...

Any chance to have a VB vesrion of the code?

Anonymous said...

I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

Sorry for offtopic

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

There was an error in this gadget