Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. private void loginButton_Click(object sender, EventArgs e)
  2. {
  3. AdamPanel blarg = new AdminPanel();
  4. string pass, user;
  5. string password = "";
  6. string username = "";
  7.  
  8. user = usernameBox.Text;
  9. pass = passwordBox.Text;
  10. DataSet bb = new DataSet();
  11.  
  12. string connectionString = "datasource=stuff;database=users";
  13. MySqlConnection mysql = new MySqlConnection(connectionString);
  14. MySqlDataAdapter adapter = new MySqlDataAdapter();
  15.  
  16. adapter.SelectCommand = new MySqlCommand("SELECT * FROM RegularUsers WHERE Username = '" + user + "' AND Password = '" + pass + "'", mysql);
  17. adapter.Fill(bb);
  18.  
  19. if(bb.HasRows)
  20. blarg.Show();
  21. return 0;
  22.  
  23.  
  24.  
  25.  
  26.  
  27. }
  28.  
  29. }
  30. }
  31.  
  32. public int Load()
  33. {
  34. string connectionString = "datasource=STUFF YOU SHOULDNT SEEdatabase=users";
  35. MySqlConnection mysql = new MySqlConnection(connectionString);
  36. MySqlDataAdapter adapter = new MySqlDataAdapter();
  37.  
  38. mysql.SelectCommand = new MySqlCommand("SELECT * FROM [RegularUsers] WHERE Username = '" + this.username + "' AND Pass = '" + this.password + "'", conn);
  39. mysql.Fill(dataset);
  40.  
  41.  
  42. if (dataset.HasRows)
  43. return 1;
  44. return 0;
  45. }
  46.  
  47. public MySqlConnection NewConnection()
  48. {
  49. string cstr = String.Format(
  50. "SERVER={0};PORT={1};DATABASE={2};UID={3};PWD={4}",
  51. sett.DBHost, sett.DBPort, sett.DBDatabase,
  52. HotelDB.user, HotelDB.password);
  53. MySqlConnection conn = new MySqlConnection(cstr);
  54. try { conn.Open(); }
  55. catch (Exception ex)
  56. {
  57. conn = null;
  58. }
  59. return conn;
  60. }
  61.  
  62.  
  63. private MySqlCommand NewCommand(MySqlConnection conn, string cmd, params object[] parameters)
  64. {
  65. if (conn==null) return null;
  66. MySqlCommand command;
  67. try
  68. {
  69. command = new MySqlCommand(cmd, conn);
  70. if (parameters != null)
  71. {
  72. int index = 0;
  73. while (index < parameters.Length)
  74. {
  75. command.Parameters.Add(
  76. new MySqlParameter((string)parameters[index], parameters[index + 1]));
  77. index += 2;
  78. }
  79. }
  80. }
  81. catch { command = null; }
  82. return command;
  83. }
  84.  
  85. private string MD5(string Value)
  86. {
  87. System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
  88. byte[] data = System.Text.Encoding.ASCII.GetBytes(Value);
  89. data = x.ComputeHash(data);
  90. string ret = "";
  91. for (int i=0; i < data.Length; i++)
  92. ret += data[i].ToString("x2").ToLower();
  93. return ret;
  94. }
  95.  
  96. MySqlConnection conn = NewConnection(.....);
  97. MySqlCommand cmd = NewCommand(conn,"SELECT userid,useractive FROM table WHERE username=@user AND MD5(password)=@pwd","user",username,"pwd",MD5(pwd));
  98. MySqlDataReader rd = cmd.ExecuteReader();
  99.  
  100. private void button Click
  101. {
  102. MySqlConnection mcon = new MySqlConnection("Yourconnection");
  103. MySqlDataAdapter adapter;
  104. DataTable table = new DataTable();
  105.  
  106. adapter = new MySqlDataAdapter("Select * From database.table where Username = '" + textbox1.Text + "' and password = '" + textbox2.Text + "'", mcon);
  107. adapter.Fill(table);
  108. if (table.Rows.Count <= 0)
  109. {
  110. //message something
  111. }
  112. else
  113. {
  114. //show main form
  115. }
  116. }
  117.  
  118. public bool ValidateLogin(string username, string password)
  119. {
  120.  
  121. MySqlConnection conn = new MySqlConnection("[YOURCONNECTIONSTRING]");
  122. MySqlDataAdapter adapter = new MySqlDataAdapter();
  123. adapter.SelectCommand = new MySqlCommand("SELECT * FROM [YOURUSERTABLE] WHERE Username = ? AND Pass = ?", conn);
  124.  
  125. adapter.SelectCommand.Parameters.Add("@Username", username);
  126. adapter.SelectCommand.Parameters.Add("@Password", password);
  127.  
  128. adapter.Fill(dataset);
  129.  
  130.  
  131. If (dataset.HasRows)
  132. {
  133. // User is logged in maybe do FormsAuthentication.SetAuthcookie(username);
  134. return true;
  135. } else {
  136. // Authentication failed
  137. return false;
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement