Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Program Security
- public class Security
- {
- public static string HashCode(string str)
- {
- string rethash = "";
- try
- {
- System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
- System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
- byte[] combined = encoder.GetBytes(str);
- hash.ComputeHash(combined);
- rethash = BitConverter.ToString(hash.Hash).Replace("-", "");
- }
- catch (Exception ex)
- {
- string strerr = "Error in HashCode : " + ex.Message;
- }
- return rethash;
- }
- }
- #endregion
- private void btnLogin_Click(object sender, EventArgs e)
- {
- try
- {
- using (MySqlConnection myConn = new MySqlConnection(strConnect))
- using (MySqlCommand selectCommand = new MySqlCommand())
- {
- string hashedvalue = Security.HashCode(txtPassword.Text);
- selectCommand.CommandText = "SELECT * FROM database WHERE column1=@User AND column2=@Password";
- selectCommand.Connection = myConn;
- selectCommand.Parameters.Add("@User", MySqlDbType.VarChar).Value = txtUsername.Text;
- selectCommand.Parameters.Add("@Password", MySqlDbType.VarChar).Value = hashedvalue;
- myConn.Open();
- MySqlDataReader myReader;
- object result = selectCommand.ExecuteScalar();
- if (result != null)
- {
- myReader = selectCommand.ExecuteReader();
- int count = Convert.ToInt32(result);
- bool IsAdminUser = false;
- while (myReader.Read())
- {
- count = count + 1;
- IsAdminUser = myReader["rank"].ToString().Equals("admin");
- }
- if (count > 0 && IsAdminUser == true)
- {
- this.Hide();
- admin.Show();
- }
- else if (count > 0)
- {
- this.Hide();
- management.Show();
- }
- else
- MessageBox.Show("Incorrect Username or Password");
- }
- else
- {
- MessageBox.Show("Incorrect Username or Password");
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement