private void savechanges(object sender, RoutedEventArgs e) { if (!string.IsNullOrWhiteSpace(currPassBox.Password) || !string.IsNullOrWhiteSpace(currPass4Name.Password) || !string.IsNullOrWhiteSpace(newPassBox.Password) || !string.IsNullOrWhiteSpace(newUsernameBox.Text)) { if (rx.IsMatch(currPass4Name.Password) || rx.IsMatch(currPassBox.Password) || rx.IsMatch(newPassBox.Password) || rx.IsMatch(newUsernameBox.Text)) { stateInfoLbl.Foreground = Brushes.Red; stateInfoLbl.Content = "Banned credentials"; } else { using (Data1Context context = new Data1Context()) { bool madeChanges = false; stateInfoLbl.Content = ""; if (Encryption.Encrypt(currPass4Name.Password, (currPass4Name.Password + Encryption.EncPass(salt)), loggedUser.UserPass).Equals(loggedUser.UserPass)) { loggedUser.UserName = newUsernameBox.Text; context.Update(loggedUser); stateInfoLbl.Foreground = Brushes.Green; stateInfoLbl.Content += " Username changed succesfully!"; madeChanges = true; } if (Encryption.Encrypt(currPassBox.Password, (currPassBox.Password + Encryption.EncPass(salt)), loggedUser.UserPass).Equals(loggedUser.UserPass)) { loggedUser.UserPass = Encryption.Encrypt(newPassBox.Password, (newPassBox.Password + Encryption.EncPass(salt))); context.Update(loggedUser); stateInfoLbl.Foreground = Brushes.Green; stateInfoLbl.Content += " Password changed succesfuly!"; madeChanges = true; } if (madeChanges) { context.SaveChanges(); currPass4Name.Password = null; newPassBox.Password = null; currPassBox.Password = null; newUsernameBox.Text = null; RenderStuff(); } } } } else { stateInfoLbl.Foreground = Brushes.Red; stateInfoLbl.Content = "Invalid password"; } }
private void Button_Click(object sender, RoutedEventArgs e) { using (Data1Context context = new Data1Context()) { if (!string.IsNullOrWhiteSpace(usernameBox.Text) || !string.IsNullOrWhiteSpace(passwordBox.Password)) { if (rx.IsMatch(usernameBox.Text)) { Message(Brushes.Red, "Banned username"); } else if (rx.IsMatch(passwordBox.Password)) { Message(Brushes.Red, "Banned password"); } else { Models.UserData user = context.UserData.Where(x => x.UserName == usernameBox.Text).FirstOrDefault(); if ((sender as Button).Name.Equals("loginButton")) { if (user != null) { if (Encryption.Encrypt(passwordBox.Password, (passwordBox.Password + Encryption.EncPass(salt)), user.UserPass).Equals(user.UserPass)) { Message(Brushes.Green, "Login Succesful"); SecondWindow.Show(); SecondWindow.GetLoggedUser(user); SecondWindow.Owner = this; this.Hide(); } else { Message(Brushes.Red, "Invalid Password"); } } else { Message(Brushes.Red, "Invalid Username"); } } else if ((sender as Button).Name.Equals("registerButton")) { if (user == null) { if (passwordBox.Password.Length > 7 && passwordBox.Password.Length < 50 && usernameBox.Text.Length < 50) { UserData newuser = new UserData { UserID = 0 + context.UserData.Count(), UserName = usernameBox.Text, UserPass = Encryption.Encrypt(passwordBox.Password, (passwordBox.Password + Encryption.EncPass(salt))) }; try { context.Add(newuser); context.SaveChanges(); Message(Brushes.Green, "Registration succesful"); } catch (Exception exc) { Message(Brushes.Red, "ERROR: \n" + exc); loginButton.IsEnabled = false; registerButton.IsEnabled = false; } } else { Message(Brushes.Red, "Password has to have at least 8 characters"); } } else { Message(Brushes.Red, "Account with this name already exists"); } } } } else { Message(Brushes.Red, "Input fields cannot be empty"); } } }