public ActionResult New(RegistrUserVM user, HttpPostedFileBase fileUpload) { if (ModelState.IsValid) { if (fileUpload != null) { var res = ImageHelper.ScaleImage(Image.FromStream(fileUpload.InputStream, true, true), 200, 200); ImageConverter _imageConverter = new ImageConverter(); byte[] xByte = (byte[])_imageConverter.ConvertTo(res, typeof(byte[])); user.Photo = xByte; } UserData newUser = new UserData(); try { newUser = dataHelper.CreateUser(user, passwordHelper.CryptPassword(user.Password)); } catch (ValidationException ve) { ModelState.AddModelError(ve.Property, ve.Message); return(View(user)); } emailHelper.SendRegistrationMessage(newUser.Login, newUser.PasswordHash, newUser.Email, EmailHelper.EmailType.Registration); logger.InfoMessage("101", $"Send conf email to {newUser.Login} : {newUser.Email}"); return(View("RegistrationLink", newUser)); //for test } else { return(View(user)); } }
public ActionResult ChangePassRegistered(ChangePassRegistered passData) { try { var user = dataHelper.GetUserData(User.Identity.Name); ViewBag.User = user; if (ModelState.IsValid) { var userPassData = dataHelper.GetPasswordData(user.Login); if (passwordHelper.CheckPassword(passData.OldPassword, userPassData)) { userPassData = passwordHelper.CryptPassword(passData.ConfirmPassword); userPassData.Login = user.Login; dataHelper.ChangePass(userPassData); return(RedirectToAction("Index")); } else { ModelState.AddModelError("OldPassword", "Не верный пароль"); return(View(passData)); } } return(View(passData)); } catch (Exception ex) { logger.ErrorMessage("305", ex); return(View("Error", new Error() { ExDescription = ex.Message })); } }
public bool ChangePassword(int userId, string userName, string oldPassword, string newPassword) { if (oldPassword == newPassword) { throw new NewPasswordCannotBeAsOneOfOldPasswordsException(); } Users user = GetUserById(userId); if (user == null) { throw new NoEntryFoundException(userId, typeof(Users).Name); } Credentials credentials = CredentialsRepository.FindByUserNameAndUserId(userId, userName); if (credentials == null) { throw new NoEntryFoundException(userId, typeof(Credentials).Name); } bool validPassword = CheckUserPassword(credentials, oldPassword); if (!validPassword) { throw new InvalidPasswordException(); } bool value = CheckForPasswordHistory(userId, credentials.Id, newPassword); if (!value) { return(false); } UserPasswordsHistory history = new UserPasswordsHistory { CredentialsId = credentials.Id, UserId = user.Id, PasswordHash = credentials.PasswordHash, PasswordSalt = credentials.PasswordSalt, ExpiredOn = DateTime.UtcNow }; ArchiveRepository.Add(history); HashedAndSaltedPassword newPasswordHash = PasswordHelper.CryptPassword(newPassword); credentials.PasswordHash = newPasswordHash.PasswordHash; credentials.PasswordSalt = newPasswordHash.PasswordSalt; CredentialsRepository.Update(credentials); return(true); }
public bool AssignUserCredentials(int userId, string userName, string password) { Users user = GetUserById(userId); if (user == null) { throw new NoEntryFoundException(userId, typeof(Users).Name); } Credentials existingCredentials = CredentialsRepository.FindByUserId(userId); if (existingCredentials != null) { throw new ExistingCredentialsFoundException(userId, existingCredentials.Id); } existingCredentials = CredentialsRepository.FindByUserName(userName); if (existingCredentials != null) { throw new ExistingCredentialsFoundException(userName); } HashedAndSaltedPassword hashAndSaltPassword = PasswordHelper.CryptPassword(password); Credentials newCredentials = new Credentials { UserId = userId, UserName = userName, PasswordHash = hashAndSaltPassword.PasswordHash, PasswordSalt = hashAndSaltPassword.PasswordSalt }; CredentialsRepository.Add(newCredentials); return(true); }