danasegarane
Sep 8th, 2008, 05:01 AM
n this code Snippet I am going to show how to use the Login Control with SQL for Authenticate UserName and password
1.Place one Login Control Named Login1
2.Declare A Public Varriable named strErrorDescription
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
Boolean bauthenticated = false;
bauthenticated = isValidUser(Login1.UserName, Login1.Password);
if (bauthenticated)
{
e.Authenticated = true;
}
else
{
e.Authenticated = false;
}
}
Code For Validating userName and Password in SQLTable
This part of the code will check the username and password from the SQLTable
private Boolean isValidUser(string username, string pwd)
{
SqlConnection con = new SqlConnection("DataBase=Northwind;Server=(local);User ID=sa;Password=test");
SqlCommand cmd = new SqlCommand("select * from users where userid='" + username + "'");// and pwd='" + pwd + "'");
cmd.Connection = con;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count == 0)
{
strLoginErrorMsg = "Invalid UserName";
dt.Dispose();
return false;
}
else
{
string strTemppwd=Convert.ToString(dt.Rows[0]["pwd"]).Trim();
if (strTemppwd.ToLower() != Login1.Password.ToLower())
{
strLoginErrorMsg = "Invalid Password";
}
}
return true;
}
OverRide the ErrorMessage
protected void Login1_LoginError(object sender, EventArgs e)
{
Login1.FailureText = strLoginErrorMsg;
}
Happy Coding :wave: :wave:
1.Place one Login Control Named Login1
2.Declare A Public Varriable named strErrorDescription
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
Boolean bauthenticated = false;
bauthenticated = isValidUser(Login1.UserName, Login1.Password);
if (bauthenticated)
{
e.Authenticated = true;
}
else
{
e.Authenticated = false;
}
}
Code For Validating userName and Password in SQLTable
This part of the code will check the username and password from the SQLTable
private Boolean isValidUser(string username, string pwd)
{
SqlConnection con = new SqlConnection("DataBase=Northwind;Server=(local);User ID=sa;Password=test");
SqlCommand cmd = new SqlCommand("select * from users where userid='" + username + "'");// and pwd='" + pwd + "'");
cmd.Connection = con;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count == 0)
{
strLoginErrorMsg = "Invalid UserName";
dt.Dispose();
return false;
}
else
{
string strTemppwd=Convert.ToString(dt.Rows[0]["pwd"]).Trim();
if (strTemppwd.ToLower() != Login1.Password.ToLower())
{
strLoginErrorMsg = "Invalid Password";
}
}
return true;
}
OverRide the ErrorMessage
protected void Login1_LoginError(object sender, EventArgs e)
{
Login1.FailureText = strLoginErrorMsg;
}
Happy Coding :wave: :wave: