Checking credentials in SQL Server (C#) : FormsAuthentication « Login Security « ASP.Net






Checking credentials in SQL Server (C#)

<%@ 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>

 








Related examples in the same category

1.FormsAuthentication.Authenticate (C#)
2.FormsAuthentication.RedirectFromLoginPage (VB)
3.Checking credentials in SQL Server (VB)
4.assigning a name to the user accessing next pages
5.FormsAuthentication.SignOut()
6.Hash password
7.Save user account to an XML file