Esempio n. 1
0
        protected override void ChangePassword(ChangePasswordParameters parameters)
        {
            Guard.ArgumentNotNull(parameters, "parameters");

            try
            {
                CustomChangePasswordEventArgs customChangePasswordEventArgs = new CustomChangePasswordEventArgs(parameters);

                CustomChangePassword?.Invoke(this, customChangePasswordEventArgs);

                if (!customChangePasswordEventArgs.Handled)
                {
                    if (!AuthenticatingEmployee.ComparePassword(parameters.OldPassword))
                    {
                        throw new Exception(String.Format("{0} {1}", SecurityExceptionLocalizer.GetExceptionMessage(SecurityExceptionId.OldPasswordIsWrong), SecurityExceptionLocalizer.GetExceptionMessage(SecurityExceptionId.RetypeTheInformation)));
                    }

                    if (parameters.NewPassword != parameters.ConfirmPassword)
                    {
                        throw new Exception(String.Format("{0} {1}", SecurityExceptionLocalizer.GetExceptionMessage(SecurityExceptionId.PasswordsAreDifferent), SecurityExceptionLocalizer.GetExceptionMessage(SecurityExceptionId.RetypeTheInformation)));
                    }

                    if (AuthenticatingEmployee.ComparePassword(parameters.NewPassword))
                    {
                        throw new Exception(String.Format("{0} {1}", SecurityExceptionLocalizer.GetExceptionMessage(SecurityExceptionId.NewPasswordIsEqualToOldPassword), SecurityExceptionLocalizer.GetExceptionMessage(SecurityExceptionId.RetypeTheInformation)));
                    }

                    KeyValuePair <HttpStatusCode, string> result = MultiTenantHelper.SetPassword(AuthenticatingEmployee, AuthenticatingEmployee, parameters.NewPassword);

                    if (result.Key == HttpStatusCode.OK)
                    {
                        AuthenticatingEmployee.SetPassword(parameters.NewPassword);
                        AuthenticatingEmployee.ChangePasswordOnFirstLogon = false;
                        this.ObjectSpace.SetModified(AuthenticatingEmployee);
                        this.ObjectSpace.CommitChanges();
                    }

                    SecurityModule.TryUpdateLogonParameters(parameters.NewPassword);

                    if (!View.ObjectSpace.IsModified)
                    {
                        bool isCurrentUser = IsCurrentUser(View.ObjectSpace, View.CurrentObject);
                        if (isCurrentUser)
                        {
                            View.ObjectSpace.ReloadObject(View.CurrentObject);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ToastMessageHelper.ShowErrorMessage(this.Application, ex, InformationPosition.Bottom);
            }
            finally
            {
                parameters.ClearValues();
            }
        }
Esempio n. 2
0
        public IdentityResult ChangePassword(ChangePasswordParameters parameters)
        {
            var identityResult = UserManager.PasswordValidator.ValidateAsync(parameters.newPassword).Result;

            if (identityResult.Succeeded)
            {
                identityResult = UserManager.ChangePassword(ApplicationUser.Id, parameters.currentPassword, parameters.newPassword);
            }
            return(identityResult);
        }
        public async Task <IActionResult> ChangePassword(
            [FromRoute] string userName,
            [FromBody] ChangePasswordParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity(ModelState));
            }

            User user = await _identityBusiness.FindByNameAsync(userName);

            if (user == null)
            {
                return(NotFound());
            }

            User loggedOnUser = await _identityBusiness.GetUserAsync(User);

            if (user.Id != loggedOnUser.Id)
            {
                return(Forbid());
            }

            IdentityResult result = await _identityBusiness.ChangePasswordAsync(loggedOnUser, parameters.CurrentPassword, parameters.NewPassword);

            if (result.Succeeded)
            {
                return(NoContent());
            }

            foreach (IdentityError error in result.Errors)
            {
                ModelState.AddModelError(error.Code, error.Description);
            }

            return(UnprocessableEntity(ModelState));
        }