Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

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!

Technorati Tags: ,
Posted on Friday, September 12, 2008 1:26 AM ADO.NET , ASP.NET | Back to top


Comments on this post: Custom Login: Validating UserName and Password using the ADO.NET way

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
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!
Left by Kyle on Sep 12, 2008 2:53 AM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
Hi,

This piece of Code was really helpful.....thanks a ton......
Left by Triveni on Jan 14, 2009 8:26 AM

# Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
its fine
Left by satish on Mar 26, 2010 8:35 PM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
this code i useful its ok
Left by rahulji on Apr 23, 2010 4:46 PM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
ok this code is useful
Left by rahulji on Apr 23, 2010 5:14 PM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
hi this rahul ........ wann friend me .............mailme rossiji_46@yahoo.co.in
Left by rahulji on Apr 23, 2010 5:18 PM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
pls give db tbl of this .
Left by Raul on Jul 26, 2011 9:31 PM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
thanks a lot
Left by kamal on Dec 14, 2011 1:53 AM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
its very good post.
thankyou very much
Left by Akbar on Nov 26, 2012 8:11 AM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
Hey I'm wishing for a Visual basic 6.0 version could you please make one?
Left by Reign from Pasig, ph on Nov 26, 2012 10:34 PM

# re: Custom Login: Validating UserName and Password using the ADO.NET way
Requesting Gravatar...
Its really very nice...thanks a lot......
Left by raji on Feb 01, 2013 1:36 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net