private void SubmitRegistration(object sender, RoutedEventArgs e) { if (String.IsNullOrEmpty(this.TxtUsername.Text) || String.IsNullOrEmpty(this.TxtEmail.Text) || String.IsNullOrEmpty(this.TxtPassword.Password)) { MessageBox.Show("Username, email and password can not be empty!"); } else if (this.TxtPassword.Password != this.TxtConfirmPassword.Password) { MessageBox.Show("Passwords are not the same!"); } else { UserSet userToAdd = new UserSet(); userToAdd.Username = this.TxtUsername.Text; userToAdd.Email = this.TxtEmail.Text; userToAdd.PasswordHash = DataAccess.PasswordHash.HashPassword(this.TxtPassword.Password); if (!String.IsNullOrEmpty(this.TxtCreditCardNumber.Text)) { userToAdd.CreditCardNumberHash = DataAccess.PasswordHash.HashPassword(this.TxtCreditCardNumber.Text); } if (!String.IsNullOrEmpty(this.TxtAddress.Text)) { userToAdd.Address = this.TxtAddress.Text; } if (!String.IsNullOrEmpty(this.TxtZipCode.Text)) { userToAdd.Address = this.TxtZipCode.Text; } if (!String.IsNullOrEmpty(this.TxtFirstName.Text)) { userToAdd.Address = this.TxtFirstName.Text; } if (!String.IsNullOrEmpty(this.TxtLastName.Text)) { userToAdd.Address = this.TxtLastName.Text; } if (!String.IsNullOrEmpty(this.TxtTicketId.Text)) { userToAdd.Address = this.TxtTicketId.Text; } if (!String.IsNullOrEmpty(this.TxtDiscountcardId.Text)) { userToAdd.Address = this.TxtDiscountcardId.Text; } if (DataAccess.DataAccess.AddUser(userToAdd)) { MessageBox.Show("Registration successful! Use your username and password to login."); MainWindow mainWindow = new MainWindow(); this.Close(); mainWindow.ShowDialog(); } else { MessageBox.Show("Oops, something went wrong in your registration!"); } } }
public static bool Login(UserSet user) { var userByUsername = Context.UserSet.FirstOrDefault(x => x.Username == user.Username); if (userByUsername != null) { return userByUsername.PasswordHash == user.PasswordHash; } var userByEmail = Context.UserSet.FirstOrDefault(x => x.Email == user.Email); return userByEmail != null && userByEmail.PasswordHash == user.PasswordHash; }
public static bool AddUser(UserSet user) { try { if (Context.UserSet.Where(x => x.Username == user.Username).ToList().Count > 0 || Context.UserSet.Where(x => x.Email == user.Email).ToList().Count > 0 || Context.UserSet.Where(x => user.CreditCardNumberHash != null && x.CreditCardNumberHash == user.CreditCardNumberHash).ToList().Count > 0 || Context.UserSet.Where(x => user.DiscountCardId != null && x.DiscountCardId == user.DiscountCardId).ToList().Count > 0) { throw new ArgumentException("User information with the same data already exists"); } if (user.Username != null && user.Email != null && user.PasswordHash != null ) { Context.UserSet.Add(user); Context.SaveChanges(); return true; } throw new ArgumentException("Username, email and password must not be empty!"); } catch (Exception exception) { MessageBox.Show(exception.Message); return false; } }