How to authenticate a user using Loginview control


A sample code that demonstrate that how can we use the Authenticate method of the ASP.NET Login Control for authenticating a user. The Methods Login1_Authenticate.

We know that the ASP.NET is having there in built LOGIN VIEW control using which we can provide the common functionality of the Login in our application. But we have to check the user credentials from our data base so that the valid user can only logs in to the site. The Login View has some Inbuilt methods which we can use to authenticate the user.

like Login1_Authenticate

In this code I am simply demonstrating that how can we make use of this control and the Authenticate Method. Here is a sample code for the same.



protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)

{

if (ValidationLogin(Login1.UserName, Login1.Password))

{

Login1.Visible = false;

MessageLabel.Text = " LogIn Successful";

}

else

{ e.Authenticated = false; }

}




in the Login1_Authenticate I am calling a function ValidationLogin which check the user entered credentials from the data base and returns a Boolean value. If the credentials are valid TRUE will be returned. else false. Based on the Return Value we are allowing the user to access the pages and go further.

Here is the ValidateLogin function where we passing the value entered by user and will check them from the value stored int the data base.


private bool ValidationLogin(string UserName, string Password)

{

bool boolReturnValue = false;

string strConnection = "server=.;database=Vendor;uid=sa;pwd=test;";

SqlConnection sqlConnection = new SqlConnection(strConnection);

String SQLQuery = "SELECT UserName, Password FROM Login";

SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);

SqlDataReader Dr;

sqlConnection.Open();

Dr = command.ExecuteReader();

while (Dr.Read())

{

if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString()))

{

boolReturnValue = true;

}

Dr.Close();

return boolReturnValue;

}

return boolReturnValue;

}

}

If the value matches with the data base value we will return TRUE else FALASE.





so this was the sample code to show that how can we use the LOGIN VIEW control to validate a user in our application.

Reference: http://www.mannpandey.blogspot.com/


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: