public async Task <IActionResult> ResetPassword(ResetPasswordViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var User = await _userManager.FindByIdAsync(viewModel.userId.ToString());

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

                await _userManager.RemovePasswordAsync(User);

                var result = await _userManager.AddPasswordAsync(User, viewModel.NewPassword);

                if (result.Succeeded)
                {
                    ViewBag.AlertSuccess = "بازنشانی کلمه عبور با موفقیت انجام شد.";
                }
                else
                {
                    ModelState.AddErrorsFromResult(result);
                }

                viewModel.Email = User.Email;
            }

            return(View(viewModel));
        }
예제 #2
0
        public async Task <IHttpActionResult> ResetPassword(ResetPasswordByAdminViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var user = await _userManager.FindByIdAsync(model.Id);

            if (user == null)//|| !(await _userManager.IsEmailConfirmedAsync(user.Id)))
            {
                // Don't reveal that the user does not exist or is not confirmed
                return(BadRequest("کاربری با مشخصات مورد نظر در سیستم موجود نیست"));
            }

            var code = await _userManager.RemovePasswordAsync(user.Id);

            var result = await _userManager.AddPasswordAsync(user.Id, model.Password);

            // If we got this far, something failed, redisplay form
            return(Ok());
        }
        public async Task <IActionResult> ResetPassword(UserResetPasswordViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.FindByIdAsync(viewModel.Id);

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

                await userManager.RemovePasswordAsync(user);

                await userManager.AddPasswordAsync(user, viewModel.NewPassword);

                ViewBag.AlertSuccess = "رمز عبور کاربر با موفقیت ریست شد";
                viewModel.UserName   = user.UserName;
                viewModel.Email      = user.Email;
            }
            return(View(viewModel));
        }
        public async Task <ActionResult> SetPassword(SetPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _userManager.AddPasswordAsync(UserId, model.NewPassword);

                if (result.Succeeded)
                {
                    var user = await _userManager.FindByIdAsync(UserId);

                    if (user != null)
                    {
                        await SignInAsync(user, isPersistent : false);
                    }
                    return(RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess }));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <ActionResult> SetPassword(SetPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _userManager.AddPasswordAsync(_userManager.GetCurrentUserId(), model.NewPassword).ConfigureAwait(false);

                if (result.Succeeded)
                {
                    var user = await _userManager.GetCurrentUserAsync().ConfigureAwait(false);

                    if (user != null)
                    {
                        await _applicationSignInManager.RefreshSignInAsync(user, isPersistent : false).ConfigureAwait(false);
                    }
                    return(RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess }));
                }
                addErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <IActionResult> ResetPassword(UserResetPasswordViewModel ViewModel)
        {
            if (ModelState.IsValid)
            {
                var User = await _userManager.FindByIdAsync(ViewModel.Id);

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

                await _userManager.RemovePasswordAsync(User);

                await _userManager.AddPasswordAsync(User, ViewModel.NewPassword);

                ViewBag.AlertSuccess = "بازنشانی کلمه عبور با موفقیت انجام شد.";
                ViewModel.UserName   = User.UserName;
                ViewModel.Email      = User.Email;
            }

            return(View(ViewModel));
        }
예제 #7
0
        public async Task <SetPasswordResult> ProcessSetPassword(string resetToken, string newPassword, bool isExpiration = false, string userName = null)
        {
            var identRes = await UsrManager.PasswordValidator.ValidateAsync(newPassword);

            if (!identRes.Succeeded)
            {
                var msgs = (new [] { "Password failed checks:" }).Concat(identRes.Errors).ToArray();
                return(new SetPasswordResult {
                    Success = false, Errors = msgs
                });
            }


            ApplicationUser user;

            if (!isExpiration)
            {
                if ((String.IsNullOrWhiteSpace(newPassword) ||
                     (user = await users.SearchFor(u => u.PasswordResetKey == resetToken).SingleOrDefaultAsync()) ==
                     null))
                {
                    return(new SetPasswordResult {
                        Success = false, Errors = new[] { "Password reset invalid" }
                    });
                }
            }
            else
            {
                if ((String.IsNullOrWhiteSpace(newPassword) ||
                     (user = await users.SearchFor(u => u.UserName == userName).FirstOrDefaultAsync()) ==
                     null))
                {
                    return(new SetPasswordResult {
                        Success = false, Errors = new[] { "Password reset invalid" }
                    });
                }
            }

            var reusingPassword = await UsrManager.FindAsync(user.UserName, newPassword) != null; // Check not reusing current password.

            if (reusingPassword)
            {
                return(new SetPasswordResult {
                    Success = false, Errors = new[] { "New password must be different than the old one." }
                });
            }

            // If this is a reset then will unlock...
            if (user.PasswordResetIsUnlock)
            {
                user.LockoutEndDateUtc     = null;
                user.PasswordResetIsUnlock = false;
            }
            else if (await UsrManager.IsLockedOutAsync(user.Id))
            {
                return(new SetPasswordResult {
                    Success = false, Errors = new[] { "Account is locked out. Please contact your administrator" }
                });
            }

            user.PasswordResetKey  = null;
            user.DatePasswordReset = user.PasswordChangedDate = DateTime.UtcNow;

            await dbApplicationContext.SaveChangesAsync();

            await UsrManager.RemovePasswordAsync(user.Id);

            identRes = await UsrManager.AddPasswordAsync(user.Id, newPassword);

            if (!identRes.Succeeded)
            {
                var msgs = (new [] { "Unable to set password:" }).Concat(identRes.Errors).ToArray();
                return(new SetPasswordResult {
                    Success = false, Errors = msgs
                });
            }
            return(new SetPasswordResult {
                Success = true
            });
        }