Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _userManager.GetUserAsync();

            if (user == null)
            {
                return(NotFound("用户不存在!"));
            }

            var addPasswordResult = await _userManager.AddPasswordAsync(user, Input.NewPassword);

            if (!addPasswordResult.Succeeded)
            {
                foreach (var error in addPasswordResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return(Page());
            }

            await _userManager.SignInManager.RefreshSignInAsync(user);

            EventLogger.LogUser("设置了密码。");
            return(RedirectToSuccessPage("你已经成功设置了密码。"));
        }
Esempio n. 2
0
        public virtual async Task <ActionResult> Manage(ManageUserViewModel model)
        {
            bool hasPassword = HasPassword();

            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl        = Url.Action("Manage");
            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess }));
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            else
            {
                // User does not have a password so remove any validation errors caused by a missing OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess }));
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <IActionResult> ChangePassword(UserModalViewModel input)
        {
            var user = _userManager.Users.Where(u => u.Id == input.Id).FirstOrDefault();

            if (user is null)
            {
                this.StatusMessage = "Error: User not found!";
                return(View("_UserStatusMessage", this.StatusMessage));
            }

            foreach (var validator in _userManager.PasswordValidators)
            {
                var result = await validator.ValidateAsync(_userManager.Instance, user, input.ConfirmPassword);

                if (!result.Succeeded)
                {
                    this.StatusMessage = $"Error: {string.Join(" ", result.Errors.Select(e => e.Description)).Replace(".", "!")}";
                    return(View("_UserStatusMessage", this.StatusMessage));
                }
            }

            if (!ModelState.IsValid)
            {
                this.StatusMessage = "Error: Passwords do not match!";
                return(View("_UserStatusMessage", this.StatusMessage));
            }

            var removeResult = await _userManager.RemovePasswordAsync(user);

            if (!removeResult.Succeeded)
            {
                this.StatusMessage = "Error: Could not remove the old password!";
                return(View("_UserStatusMessage", this.StatusMessage));
            }
            var addPasswordResult = await _userManager.AddPasswordAsync(user, input.NewPassword);

            if (!addPasswordResult.Succeeded)
            {
                this.StatusMessage = "Error: Could not change the password!";
                return(View("_UserStatusMessage", this.StatusMessage));
            }
            this.StatusMessage = "The user's password has been changed!";
            return(View("_UserStatusMessage", this.StatusMessage));
        }
        public async Task <IActionResult> SetPassword(SetPasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await GetCurrentUserAsync();

            if (user != null)
            {
                var result = await _userManager.AddPasswordAsync(user, model.NewPassword);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToAction(nameof(Index), new { Message = ManageMessageId.SetPasswordSuccess }));
                }
                AddErrors(result);
                return(View(model));
            }
            return(RedirectToAction(nameof(Index), new { Message = ManageMessageId.Error }));
        }
        private async Task <Result> AddPassword(AddPasswordDto addPasswordDto, User user)
        {
            var identityResult = await _userManager.AddPasswordAsync(user, addPasswordDto.Password);

            return(identityResult.ToResult());
        }