Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 123, comments - 343, trackbacks - 0

My Links

News

Archives

Image Galleries

Custom Login: Validating UserName and Password using the ADO.NET way

The snippet below describes on how we are going to validate the user credentials being supplied by the end user in Login page using the ADO.NET way..

C#

protected void ValidateUserInfo(string user, string pass)
{
  
    SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE");
    string sql = "SELECT * FROM TableName WHERE UserID = @username AND Password = @password";
    SqlCommand cmd = new SqlCommand(sql,connection);
    cmd.Parameters.AddWithValue("@username", user);
    cmd.Parameters.AddWithValue("@password", pass);
    connection.Open();
 
    DataTable dt = new DataTable();
    SqlDataAdapter ad = new SqlDataAdapter(cmd);
    ad.Fill(dt);
    if (dt.Rows.Count > 0) { //check if the query returns any data
        //Valid Username and Password
        Response.Redirect("Default.aspx");
    }
    else
    {
        Response.Write("INVALID Username and Password, Try Again!");
    }
    connection.Close();   
}
protected void Button1_Click(object sender, EventArgs e)
{
  ValidateUserInfo(TextUserName.Text.Trim(), TextPassword.Text.Trim());
}

VB.NET

Protected Sub ValidateUserInfo(ByVal user As String, ByVal pass As String)
   
    Dim connection As New SqlConnection("YOUR CONNECTION STRING HERE")
    Dim sql As String = "SELECT * FROM TableName WHERE UserID = @username AND Password = @password"
    Dim cmd As New SqlCommand(sql, connection)
    cmd.Parameters.AddWithValue("@username", user)
    cmd.Parameters.AddWithValue("@password", pass)
    connection.Open()
   
    Dim dt As New DataTable()
    Dim ad As New SqlDataAdapter(cmd)
    ad.Fill(dt)
    If dt.Rows.Count > 0 Then
        'check if the query returns any data
        Response.Redirect("Default.aspx")
    Else
        Response.Write("INVALID Username and Password, Try Again!")
    End If
    connection.Close()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1_Click
    ValidateUserInfo(TextUserName.Text.Trim(), TextPassword.Text.Trim())
End Sub

That simple! Happy Coding!

Print | posted on Friday, September 12, 2008 1:26 AM |

Feedback

Gravatar

# re: Custom Login: Validating UserName and Password using the ADO.NET way

That would assume that you're storing your password in plain-text in the database.

What I like to do is when a user changes their password (or enters it for the first time), use system.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5"). This makes the password into an MD5 hash. Then, whenever you validate for that password, validate against the hash instead.

Good Luck!
9/12/2008 2:53 AM | Kyle
Gravatar

# re: Custom Login: Validating UserName and Password using the ADO.NET way

Hi,

This piece of Code was really helpful.....thanks a ton......
1/14/2009 8:26 AM | Triveni
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: