private void changePassword_Click(object sender, EventArgs e) { var row = usersDGV.SelectedRows[0]; var id = row.Cells[UserIDColumn.Name].Value as int?; if (!id.HasValue || id.Value == 0) { return; } using (var passwordManager = new PasswordManagerForm(true)) { if (passwordManager.ShowDialog(this) != DialogResult.OK) { return; } using (var context = new UsersModel()) { var tag = row.Tag as Пользователи; context.Пользователи.Attach(tag); tag.Соль = PasswordSecurity.GenerateSalt(); tag.Хэш_пароля = PasswordSecurity.GenerateHash(passwordManager.Password, tag.Соль); context.SaveChanges(); row.Cells[HashColumn.Name].Value = tag.Хэш_пароля; row.Cells[SaltColumn.Name].Value = tag.Соль; row.Tag = tag; } } }
private void usersDGV_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { var row = usersDGV.Rows[e.RowIndex]; using (var context = new UsersModel()) { var id = row.Cells[UserIDColumn.Name].Value as int?; var tag = row.Tag as Пользователи; var login = row.Cells[LoginColumn.Name].Value as string; e.Cancel = login == null || login.Replace(" ", "") == "" || login.Length > 50; if (e.Cancel) { row.Cells[LoginColumn.Name].ErrorText = ManagementLocalization.IncorrectLoginError; return; } e.Cancel = context.Пользователи.Any(user => user.ID != id.Value && user.Логин == login); if (e.Cancel) { row.ErrorText = ManagementLocalization.LoginExistsError; return; } var isInsert = tag == null; if (isInsert) { tag = new Пользователи(); using (var passwordManager = new PasswordManagerForm(false)) { if (passwordManager.ShowDialog() == DialogResult.OK) { tag.Соль = PasswordSecurity.GenerateSalt(); tag.Хэш_пароля = PasswordSecurity.GenerateHash(passwordManager.Password, tag.Соль); } } } else { context.Пользователи.Attach(tag); } if (tag.Логин == AdminLogin) { AdminLogin = login; userLoginLabel.Text = userLoginLabel.Tag as string + AdminLogin; } tag.Логин = login; tag.Администратор = (bool)row.Cells[IsAdminColumn.Name].Value; tag.Дата_регистрации = ((DateTime)row.Cells[RegDateColumn.Name].Value).Date; row.Tag = tag; if (isInsert) { context.Пользователи.Add(tag); } context.SaveChanges(); } row.ErrorText = ""; foreach (DataGridViewCell cell in row.Cells) { cell.ErrorText = ""; } }
public ActionResult Register(LoginDetailsViewModel registrationDetails) { if (!ModelState.IsValid) { return(View(registrationDetails)); } ResumeBuilderDBContext dbContext = new ResumeBuilderDBContext(); if (dbContext.Logins.Any(m => m.Username == registrationDetails.UserName)) { ModelState.AddModelError("", "User already exists."); return(View(registrationDetails)); } else { try { byte[] saltBytes = PasswordSecurity.GenerateSalt(); string saltString = Convert.ToBase64String(saltBytes); byte[] hashedPasswordBytes = PasswordSecurity.ComputeHMAC_SHA256(Encoding.UTF8.GetBytes(registrationDetails.Password), saltBytes); string hashedPasswordString = Convert.ToBase64String(hashedPasswordBytes); UserDetails newUser = new UserDetails { DateOfBirth = DateTime.Now }; Login newLogin = new Login { Username = registrationDetails.UserName, Password = hashedPasswordString, Salt = saltString, UserDetails = newUser }; dbContext.Logins.Add(newLogin); dbContext.SaveChanges(); } catch (Exception) { ModelState.AddModelError("", "Something went wrong try again."); return(View(registrationDetails)); } } // If we got this far, something failed, redisplay form return(View(registrationDetails)); }
public ActionResult Register(LoginDetailsVM registrationDetails) { if (!ModelState.IsValid) { ModelState.AddModelError("", "Either username or password is empty."); return(View(registrationDetails)); } if (dbContext.Logins.Any(m => m.Username == registrationDetails.UserName)) { ModelState.AddModelError("", "User already exists."); return(View()); } else { try { string salt = PasswordSecurity.GenerateSalt(); string hashedPassword = PasswordSecurity.HashPassword(registrationDetails.Password, salt); UserDetails newUser = new UserDetails { DateOfBirth = DateTime.Now }; Login newLogin = new Login { Username = registrationDetails.UserName, Password = hashedPassword, Salt = salt, UserDetails = newUser }; newLogin.UserDetails.Setting = new Setting(); dbContext.Logins.Add(newLogin); dbContext.SaveChanges(); return(RedirectToAction("Login", "Account")); } catch (Exception) { ModelState.AddModelError("", "Something went wrong try again."); return(View(registrationDetails)); } } }
private void FinishButton_Click_OnRegistration(object sender, EventArgs e) { using (var context = new UsersModel()) { var salt = PasswordSecurity.GenerateSalt(); var user = new Пользователи { Логин = loginTextBox.Text, Хэш_пароля = PasswordSecurity.GenerateHash(passwordTextBox.Text, salt), Дата_регистрации = DateTime.Now.Date, Соль = salt, Администратор = false }; context.Пользователи.Add(user); context.SaveChanges(); usersComboBox.DataSource = context.Пользователи; MessageBox.Show(AuthenticationLocalization.RegistrationCompleted); UserLogin = user.Логин; UserType = UserTypes.User; DialogResult = DialogResult.OK; } }