Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 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. private void signInButton_Click(object sender, EventArgs e)
  34. {
  35. DataProcedures data = new DataProcedures();
  36. User userInfo = new User(usernameTextbox.Text, passwordTextbox.Text);
  37. userInfo.userId = data.verifyUser(userInfo);
  38.  
  39. if (userInfo.userId != -1)
  40. {
  41. AppWindow a = new AppWindow();
  42. a.Show();
  43. }
  44. else
  45. {
  46. errorLabel.Show();
  47. }
  48. }
  49.  
  50. public int verifyUser(User userInfo)
  51. {
  52. MySqlConnection conn = new MySqlConnection(connectionString);
  53.  
  54. int userId = -1;
  55.  
  56. string returnedUserName;
  57. string returnedPassword;
  58.  
  59. try
  60. {
  61. conn.Open();
  62. MySqlCommand checkUserNameCmd = conn.CreateCommand();
  63. checkUserNameCmd.CommandText = "SELECT EXISTS(SELECT userName FROM user WHERE userName = @username)";
  64. checkUserNameCmd.Parameters.AddWithValue("@username", userInfo.username);
  65. returnedUserName = checkUserNameCmd.ExecuteScalar().ToString();
  66.  
  67. MySqlCommand checkPasswordCmd = conn.CreateCommand();
  68. checkPasswordCmd.CommandText = "SELECT EXISTS(SELECT password FROM user WHERE BINARY password = @password AND userName = @username)";//"BINARY" is used for case sensitivity in SQL queries
  69. checkPasswordCmd.Parameters.AddWithValue("@password", userInfo.password);
  70. checkPasswordCmd.Parameters.AddWithValue("@username", userInfo.username);
  71. returnedPassword = checkPasswordCmd.ExecuteScalar().ToString();
  72.  
  73.  
  74.  
  75. if (returnedUserName == "1" && returnedPassword == "1")
  76. {
  77. MySqlCommand returnUserIdCmd = conn.CreateCommand();
  78. returnUserIdCmd.CommandText = "SELECT userId FROM user WHERE BINARY password = @password AND userName = @username";
  79. returnUserIdCmd.Parameters.AddWithValue("@password", userInfo.password);
  80. returnUserIdCmd.Parameters.AddWithValue("@username", userInfo.username);
  81. userId = (int)returnUserIdCmd.ExecuteScalar();
  82. }
  83.  
  84. }
  85. catch (Exception ex)
  86. {
  87. Console.WriteLine("Exception thrown verifying user: " + ex);
  88. }
  89. finally
  90. {
  91. conn.Close();
  92. }
  93.  
  94. return userId;
  95. }
  96.  
  97. //Need to provide email and password to this method
  98. public void Login(string email, string password)
  99. {
  100. const string query = "SELECT 1 FROM UsersTBL WHERE Email = @email AND UserPassword = @password";
  101. if (!string.IsNullOrWhiteSpace(email) && !string.IsNullOrWhiteSpace(password))
  102. {
  103. //Shouldn't need to create an instance of a Form simply to query the database
  104. SqlConnection con = null;
  105. using (var login = new LoginWindow())
  106. con = login.con;
  107. try
  108. {
  109. con.Open();
  110. var com = con.CreateCommand();
  111. com.CommandText = query;
  112. //Correct types if not VARCHAR
  113. com.Parameters.Add("@email", SqlDbType.VarChar);
  114. com.Parameters["@email"].Value = email;
  115. com.Parameters.Add("@password", SqlDbType.VarChar);
  116. //Should NOT be storing passwords as plain text in the database
  117. com.Parameters["@password"].Value = password;
  118. if (com.ExecuteScalar() == 1)
  119. {
  120. AppWindow a = new AppWindow();
  121. a.Show();
  122. }
  123. }
  124. catch (Exception e)
  125. {
  126. //log e somehow
  127. }
  128. finally
  129. {
  130. //Close the connection if still open
  131. if (con != null && con.State != ConnectionState.Closed)
  132. con.Close();
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement