title: Forms authentication and authorization that uses custom database tables? Asp.net web forms(C#).
body: <p>Hi i am currently creating a web forms asp.net website, i am trying to  add forms authentication with the following code for the login page below but i am now unable to login. I have all the appropriate code in the HTML and the web.config file and i am not receiving any errors. I may be going about the authentication for web forms wrong, any help would be appreciated. Thank You</p>
  
  <pre><code>private bool ValidateUser(string Username, string Password)
      {
          SqlConnection conn = new      SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
          SqlCommand cmd;
          string lookupPassword = null;
  
          // Check for invalid userName.
          // userName must not be null and must be between 1 and 15 characters.
          if ((null == Username) || (0 == Username.Length) || (Username.Length &gt; 15))
          {
              System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.");
              return false;
          }
  
          // Check for invalid passWord.
          // passWord must not be null and must be between 1 and 25 characters.
          if ((null == Password) || (0 == Password.Length) || (Password.Length &gt; 25))
          {
              System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.");
              return false;
          }
  
          try
          {
              // Consult with your SQL Server administrator for an appropriate connection
              // string to use to connect to your local SQL Server.
  
              conn.Open();
  
              // Create SqlCommand to select pwd field from users table given supplied userName.
              cmd = new SqlCommand("Select password from customer where Username=@userName", conn);
              cmd.Parameters.AddWithValue("@userName", TextBoxPassword.Text);
              cmd.Parameters["@userName"].Value = Username;
  
              // Execute command and fetch pwd field into lookupPassword string.
              lookupPassword = (string)cmd.ExecuteScalar();
  
              // Cleanup command and connection objects.
              cmd.Dispose();
              conn.Dispose();
          }
          catch (Exception ex)
          {
              // Add error handling here for debugging.
              // This error message should not be sent back to the caller.
              System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
          }
  
          // If no password found, return false.
          if (null == lookupPassword)
          {
              // You could write failed login attempts here to event log for additional security.
              return false;
          }
  
          // Compare lookupPassword and input passWord, using a case-sensitive comparison.
          return (0 == string.Compare(lookupPassword, Password, false));
  
      }
  
      private void cmdLogin_ServerClick(object sender, System.EventArgs e)
      {
          if (ValidateUser(TextBoxUserN.Text, TextBoxPassword.Text))
              FormsAuthentication.RedirectFromLoginPage(TextBoxUserN.Text,
                  chkPersistCookie.Checked);
          else
              Response.Redirect("login.aspx", true);
      }
  </code></pre>