/// <summary>
        /// Change password
        /// </summary>
        /// <param name="request">Request</param>
        /// <returns>Result</returns>
        public virtual PasswordChangeResult ChangePassword(ChangePasswordRequest request)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            var result = new PasswordChangeResult();
            if (String.IsNullOrWhiteSpace(request.Email))
            {
                result.AddError("Email is not provided");
                return result;
            }
            if (String.IsNullOrWhiteSpace(request.NewPassword))
            {
                result.AddError("Password is not provided");
                return result;
            }

            var account = _accountService.GetAccountByEmail(request.Email);
            if (account == null)
            {
                result.AddError("Email not found");
                return result;
            }

            var requestIsValid = false;
            if (request.ValidateRequest)
            {
                //password
                string oldPwd = "";
                switch (account.PasswordFormat)
                {
                    case PasswordFormat.Encrypted:
                        oldPwd = _encryptionService.EncryptText(request.OldPassword);
                        break;
                    case PasswordFormat.Hashed:
                        oldPwd = _encryptionService.CreatePasswordHash(request.OldPassword, account.PasswordSalt);
                        break;
                    default:
                        oldPwd = request.OldPassword;
                        break;
                }

                bool oldPasswordIsValid = oldPwd == account.Password;
                if (!oldPasswordIsValid)
                    result.AddError("Old password doesn't match");

                if (oldPasswordIsValid)
                    requestIsValid = true;
            }
            else
                requestIsValid = true;

            //at this point request is valid
            if (requestIsValid)
            {
                switch (request.NewPasswordFormat)
                {
                    case PasswordFormat.Clear:
                        {
                            account.Password = request.NewPassword;
                        }
                        break;
                    case PasswordFormat.Encrypted:
                        {
                            account.Password = _encryptionService.EncryptText(request.NewPassword);
                        }
                        break;
                    case PasswordFormat.Hashed:
                        {
                            string saltKey = _encryptionService.CreateSaltKey(5);
                            account.PasswordSalt = saltKey;
                            account.Password = _encryptionService.CreatePasswordHash(request.NewPassword, saltKey);
                        }
                        break;
                    default:
                        break;
                }
                account.PasswordFormat = request.NewPasswordFormat;
                _accountService.UpdateAccount(account);
            }

            return result;
        }
Ejemplo n.º 2
0
 public async Task<ActionResult> UserProfile(ResetPasswordViewModel model)
 {
    // var user = _accountService.GetAccountByEmail(User.Identity.Name);
     DataSettingsHelper.ResetCache();
     var user =  _accountService.GetAccountByEmail(model.Email);
     if (user == null)
     {
         // Don't reveal that the user does not exist
         return RedirectToAction("ResetPasswordConfirmation", "Account");
     }
     const PasswordFormat passworkFormat = new PasswordFormat();
     var changePasswordRequest = new ChangePasswordRequest(model.Email, true, passworkFormat, model.OldPassword);
     // _accountService.InsertGuestAccount
     var registrationResult = _accountRegistrationService.ChangePassword(changePasswordRequest);
     //var result = await UserManager.ResetPasswordAsync(model.Email, model.OldPassword, model.Password);
   
         return RedirectToAction("Index", "Home");
     
  
    
     return View(model);
 }