C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Validate username and Password using Login Control and SQL


Posted Date: 27 Aug 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms

Posted By: Danasegarane.A       Member Level: Diamond
Rating:     Points: 10



In this code Snippet I am going to Explain 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;
}





Hope this helps

Dana




Responses

Author: Babu    28 Nov 2008Member Level: Bronze   Points : 1
how to validate the username and password with database(using vb) in login control... can you send the example?.

Thanks in advance.


Author: Babu    28 Nov 2008Member Level: Bronze   Points : 1
how to validate the username and password with database(using vb) in login control... can you send the example?.

Thanks in advance.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Validate username and Password using Login Control and SQL  .  SQL  .  LoginControl  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Accessing Controls on Master Pages from Content Pages
Previous Resource: On Before Unload
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

masks masks masks

Contact Us    Privacy Policy    Terms Of Use