Advertisement
ra1n

Login Button using MySQL w/ Parameters

Dec 26th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1.         #region Program Security
  2.         public class Security
  3.         {
  4.             public static string HashCode(string str)
  5.             {
  6.                 string rethash = "";
  7.                 try
  8.                 {
  9.                     System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
  10.                     System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
  11.                     byte[] combined = encoder.GetBytes(str);
  12.                     hash.ComputeHash(combined);
  13.                     rethash = BitConverter.ToString(hash.Hash).Replace("-", "");
  14.                 }
  15.                 catch (Exception ex)
  16.                 {
  17.                     string strerr = "Error in HashCode : " + ex.Message;
  18.                 }
  19.                 return rethash;
  20.             }
  21.         }
  22.         #endregion
  23.  
  24.         private void btnLogin_Click(object sender, EventArgs e)
  25.         {
  26.             try
  27.             {
  28.                 using (MySqlConnection myConn = new MySqlConnection(strConnect))
  29.                 using (MySqlCommand selectCommand = new MySqlCommand())
  30.                 {
  31.                     string hashedvalue = Security.HashCode(txtPassword.Text);
  32.                     selectCommand.CommandText = "SELECT * FROM database WHERE column1=@User AND column2=@Password";
  33.                     selectCommand.Connection = myConn;
  34.                     selectCommand.Parameters.Add("@User", MySqlDbType.VarChar).Value = txtUsername.Text;
  35.                     selectCommand.Parameters.Add("@Password", MySqlDbType.VarChar).Value = hashedvalue;
  36.                     myConn.Open();
  37.                     MySqlDataReader myReader;
  38.                     object result = selectCommand.ExecuteScalar();
  39.                     if (result != null)
  40.                     {
  41.                         myReader = selectCommand.ExecuteReader();
  42.                         int count = Convert.ToInt32(result);
  43.                         bool IsAdminUser = false;
  44.                         while (myReader.Read())
  45.                         {
  46.                             count = count + 1;
  47.                             IsAdminUser = myReader["rank"].ToString().Equals("admin");
  48.                         }
  49.                         if (count > 0 && IsAdminUser == true)
  50.                         {
  51.                             this.Hide();
  52.                             admin.Show();
  53.                         }
  54.                         else if (count > 0)
  55.                         {
  56.                             this.Hide();
  57.                             management.Show();
  58.                         }
  59.                         else
  60.                             MessageBox.Show("Incorrect Username or Password");
  61.                     }
  62.                     else
  63.                     {
  64.                         MessageBox.Show("Incorrect Username or Password");
  65.                     }
  66.                 }
  67.             }
  68.             catch (Exception ex)
  69.             {
  70.                 MessageBox.Show(ex.Message);
  71.             }
  72.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement