Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. public void Login()
  2. {
  3. LoginWindow l = new LoginWindow();
  4. if (l.tbxEmail.Text != "" && l.tbxPassword.Text != "")
  5. {
  6. string query = "SELECT * FROM UsersTBL";
  7. l.con.Open();
  8. l.com = l.con.CreateCommand();
  9. l.com.CommandText = query;
  10. SqlDataReader dr = l.com.ExecuteReader();
  11. if (dr.Read())
  12. {
  13. if (dr["Email"].Equals(l.tbxEmail.Text.ToString()) && dr["UserPassword"].Equals(l.tbxPassword.Text.ToString()))
  14. {
  15. AppWindow a = new AppWindow();
  16. a.Show();
  17. }
  18. else
  19. l.lblMissingParameter.Content = "Incorrect Password or Email entered";
  20. }
  21. }
  22. }
  23.  
  24. public partial class LoginWindow:Window
  25. {
  26. User u = new User();
  27. private void BtnSignup_Click(object sender, RoutedEventArgs e)
  28. {
  29. u.Login();
  30. }
  31. }
  32.  
  33. public bool Login(SqlConnection con, string email, string password)
  34. {
  35. const string query = "SELECT 1 FROM UsersTBL WHERE Email = @email AND UserPassword = @password";
  36. if (!string.IsNullOrWhiteSpace(email) && !string.IsNullOrWhiteSpace(password))
  37. {
  38. try
  39. {
  40. con.Open();
  41. var cmd = con.CreateCommand();
  42. cmd.CommandText = query;
  43. //Correct SqlDbTypes if necessary
  44. cmd.Parameters.Add("@email", SqlDbType.VarChar);
  45. cmd.Parameters["@email"].Value = email;
  46. cmd.Parameters.Add("@password", SqlDbType.VarChar);
  47. //Should NOT be storing passwords as plain text in the database
  48. cmd.Parameters["@password"].Value = password;
  49. if (cmd.ExecuteScalar() == 1)
  50. return true;
  51. }
  52. catch (Exception e)
  53. {
  54. //log e somehow or eliminate this catch block
  55. }
  56. finally
  57. {
  58. //Close the connection if still open
  59. if (con != null && con.State != ConnectionState.Closed)
  60. con.Close();
  61. }
  62. }
  63. return false;
  64. }
  65.  
  66. public partial class LoginWindow : Window
  67. {
  68. private void BtnSignup_Click(object sender, RoutedEventArgs e)
  69. {
  70. var u = new User();
  71. if (u.Login(con, tbxEmail.Text, tbxPassword.Text))
  72. {
  73. AppWindow a = new AppWindow();
  74. a.Show();
  75. }
  76. else
  77. lblMissingParameter.Content = "Incorrect Password or Email entered";
  78. }
  79. }
  80.  
  81. private void signInButton_Click(object sender, EventArgs e)
  82. {
  83. DataProcedures data = new DataProcedures();
  84. User userInfo = new User(usernameTextbox.Text, passwordTextbox.Text);
  85. userInfo.userId = data.verifyUser(userInfo);
  86.  
  87. if (userInfo.userId != -1)
  88. {
  89. AppWindow a = new AppWindow();
  90. a.Show();
  91. }
  92. else
  93. {
  94. errorLabel.Show();
  95. }
  96. }
  97.  
  98. public int verifyUser(User userInfo)
  99. {
  100. MySqlConnection conn = new MySqlConnection(connectionString);
  101.  
  102. int userId = -1;
  103.  
  104. string returnedUserName;
  105. string returnedPassword;
  106.  
  107. try
  108. {
  109. conn.Open();
  110. MySqlCommand checkUserNameCmd = conn.CreateCommand();
  111. checkUserNameCmd.CommandText = "SELECT EXISTS(SELECT userName FROM user WHERE userName = @username)";
  112. checkUserNameCmd.Parameters.AddWithValue("@username", userInfo.username);
  113. returnedUserName = checkUserNameCmd.ExecuteScalar().ToString();
  114.  
  115. MySqlCommand checkPasswordCmd = conn.CreateCommand();
  116. checkPasswordCmd.CommandText = "SELECT EXISTS(SELECT password FROM user WHERE BINARY password = @password AND userName = @username)";//"BINARY" is used for case sensitivity in SQL queries
  117. checkPasswordCmd.Parameters.AddWithValue("@password", userInfo.password);
  118. checkPasswordCmd.Parameters.AddWithValue("@username", userInfo.username);
  119. returnedPassword = checkPasswordCmd.ExecuteScalar().ToString();
  120.  
  121.  
  122.  
  123. if (returnedUserName == "1" && returnedPassword == "1")
  124. {
  125. MySqlCommand returnUserIdCmd = conn.CreateCommand();
  126. returnUserIdCmd.CommandText = "SELECT userId FROM user WHERE BINARY password = @password AND userName = @username";
  127. returnUserIdCmd.Parameters.AddWithValue("@password", userInfo.password);
  128. returnUserIdCmd.Parameters.AddWithValue("@username", userInfo.username);
  129. userId = (int)returnUserIdCmd.ExecuteScalar();
  130. }
  131.  
  132. }
  133. catch (Exception ex)
  134. {
  135. Console.WriteLine("Exception thrown verifying user: " + ex);
  136. }
  137. finally
  138. {
  139. conn.Close();
  140. }
  141.  
  142. return userId;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement