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 FinishButton_Click_OnAuthentication(object sender, EventArgs e) { if (guestCheckBox.Checked) { MessageBox.Show(AuthenticationLocalization.GuestMessage, AuthenticationLocalization.GuestMessageHeader); UserLogin = AuthenticationLocalization.GuestLogin; UserType = UserTypes.Guest; DialogResult = DialogResult.OK; return; } using (var context = new UsersModel()) { var user = usersComboBox.SelectedItem as Пользователи; if (user == null) { MessageBox.Show($@"{AuthenticationLocalization.AuthenticationError}{AuthenticationLocalization.AuthenticationLoginError}"); } context.Пользователи.Attach(user); UserLogin = user.Логин; UserType = user.Администратор ? UserTypes.Admin : UserTypes.User; var generatedHash = PasswordSecurity.GenerateHash(passwordTextBox.Text, user.Соль); if (!generatedHash.Equals(user.Хэш_пароля)) { MessageBox.Show($@"{AuthenticationLocalization.AuthenticationError} {AuthenticationLocalization.AuthenticationPasswordError}"); return; } MessageBox.Show(AuthenticationLocalization.AuthenticationCompleted); DialogResult = DialogResult.OK; } }
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 = ""; } }
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; } }