Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

ASP.Net Checking credentials in SQL server

authentication

  • Please log in to reply
No replies to this topic

#1 QuackWare

QuackWare

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 88 posts

Posted 03 January 2010 - 06:36 PM

Originally Posted Here:

ASP.Net | Login Security | FormsAuthentication | Checking credentials in SQL Server © | Free Code Snippets

/** This code snippet was taken from freecodesnippets.com*/

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
       SqlConnection conn;
       SqlCommand cmd;
       string cmdString = "SELECT [Password] FROM [AccessTable] WHERE" +
          " (([Username] = @Username) AND ([Password] = @Password))";

       conn = new SqlConnection("Data Source=localhost;Initial " +
          "Catalog=Northwind;Persist Security Info=True;User ID=sa");
       cmd = new SqlCommand(cmdString, conn);

       cmd.Parameters.Add("@Username", SqlDbType.VarChar, 50);
       cmd.Parameters["@Username"].Value = TextBox1.Text;
       cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50);
       cmd.Parameters["@Password"].Value = TextBox2.Text;

       conn.Open();

       SqlDataReader myReader;

       myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

       if (myReader.Read()) {
          FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);
       }
       else {
          Response.Write("Invalid credentials");
       }

       myReader.Close(); 
    }
</script>

  • 0





Also tagged with one or more of these keywords: authentication

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download