예제 #1
0
        public async Task <IActionResult> ChangePassword(ChangePasswordViewModel viewModel)
        {
            var user = await userManager.GetUserAsync(User);

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

            if (ModelState.IsValid)
            {
                var changePassResult = await userManager.ChangePasswordAsync(user, viewModel.OldPassword, viewModel.NewPassword);

                if (changePassResult.Succeeded)
                {
                    ViewBag.Alert = "کلمه عبور شما با موفقیت تغییر کرد";
                }
                else
                {
                    foreach (var item in changePassResult.Errors)
                    {
                        ModelState.AddModelError(string.Empty, item.Description);
                    }
                }
            }
            UserSidebarViewModel userSidebar = UpdateUserSideBar(user);

            viewModel.UserSidebar = userSidebar;
            return(View(viewModel));
        }
예제 #2
0
        public async Task <IActionResult> ChangePassword(ChangePasswordViewModel ViewModel)
        {
            var user = await _userManager.GetUserAsync(User);

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

            if (ModelState.IsValid)
            {
                var changePassResult = await _userManager.ChangePasswordAsync(user, ViewModel.OldPassword, ViewModel.NewPassword);

                if (changePassResult.Succeeded)
                {
                    ViewBag.Alert = "کلمه عبور شما با موفقیت تغییر یافت.";
                }

                else
                {
                    ModelState.AddErrorsFromResult(changePassResult);
                }
            }

            return(View(ViewModel));
        }
예제 #3
0
        public virtual async Task <ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(PartialView(viewName: MVC.admin.Shared.Views._alert, model: new AlertViewModel {
                    Alert = AlertOperation.SurveyOperation(StatusOperation.Invalid), Status = AlertMode.warning
                }));
            }


            var result = await _userManager.ChangePasswordAsync(User.Identity.GetUserId <int>(), model.OldPassword, model.NewPassword);

            if (result.Succeeded)
            {
                var user = await _userManager.FindByIdAsync(User.Identity.GetUserId <int>());

                if (user != null)
                {
                    await signInAsync(user, isPersistent : false);
                }
                return(PartialView(viewName: MVC.admin.Shared.Views._alert, model: new AlertViewModel {
                    Alert = AlertOperation.SurveyOperation(StatusOperation.SuccessChangePassword), Status = AlertMode.success
                }));
            }
            else
            {
                return(PartialView(viewName: MVC.admin.Shared.Views._alert, model: new AlertViewModel {
                    Alert = AlertOperation.SurveyOperation(StatusOperation.ChangePasswordError), Status = AlertMode.warning
                }));
            }
        }
        public async Task <IActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var user = await _userManager.GetCurrentUserAsync();

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

                var result = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);

                if (result.Succeeded)
                {
                    await _userManager.UpdateSecurityStampAsync(user);

                    await _signInManager.RefreshSignInAsync(user);

                    await _email.SendEmailAsync(
                        email : user.Email,
                        subject : "اطلاع رسانی تغییر کلمه‌ی عبور",
                        viewNameOrPath : "~/Areas/Identity/Views/EmailTemplates/_ChangePasswordNotification.cshtml",
                        model : new ChangePasswordNotificationViewModel
                    {
                        User            = user,
                        EmailSignature  = _appSettings.Value.Smtp.FromName,
                        MessageDateTime = DateTime.UtcNow.ToLongPersianDateTimeString()
                    });
                }
            }
            return(View(model));
        }
예제 #5
0
        public virtual async Task <ActionResult> ChangeUserPassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("ChangeUserPassword", model));
            }
            var result = await _userManager.ChangePasswordAsync(_userManager.GetCurrentUserId(), model.OldPassword, model.NewPassword);

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

                if (user != null)
                {
                    await signInAsync(user, isPersistent : false);
                }
                ModelState.AddModelError("کلمه عبور با موفقیت ویرایش شد", "");
            }
            addErrors(result);
            return(View("ChangeUserPassword", model));
        }
예제 #6
0
        public async Task <SetPasswordResult> ChangePassword(string userId, string oldPassword, string newPassword)
        {
            if (String.IsNullOrWhiteSpace(userId))
            {
                throw new ArgumentNullException(nameof(userId));
            }
            if (String.IsNullOrWhiteSpace(oldPassword))
            {
                throw new ArgumentNullException(nameof(oldPassword));
            }
            if (String.IsNullOrWhiteSpace(newPassword))
            {
                throw new ArgumentNullException(nameof(newPassword));
            }

            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
                });
            }
            // Check not reusing current password.
            ApplicationUser user = await UsrManager.FindByIdAsync(userId);

            var reusingPassword = await UsrManager.FindAsync(user.UserName, newPassword) != null;

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

            identRes = await UsrManager.ChangePasswordAsync(userId, oldPassword, newPassword);

            if (!identRes.Succeeded)
            {
                var msgs = (new [] { "Password change failed:" }).Concat(identRes.Errors).ToArray();
                return(new SetPasswordResult {
                    Success = false, Errors = msgs
                });
            }
            user.DatePasswordReset = DateTime.UtcNow;
            await UsrManager.UpdateAsync(user);

            return(new SetPasswordResult {
                Success = true
            });
        }
예제 #7
0
        public virtual async Task <ActionResult> ChangeUserPassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("ChangeUserPassword", model));
            }
            var result = await _userManager.ChangePasswordAsync(_userManager.GetCurrentUserId(), model.OldPassword, model.NewPassword);

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

                if (user != null)
                {
                    await signInAsync(user, isPersistent : false);
                }
                TempData["message"] = "کلمه عبور با موفقیت ویرایش شد!";
                return(RedirectToAction("Index", "Home", routeValues: new { Area = "User" }));
            }
            addErrors(result);
            return(View("ChangeUserPassword", model));
        }
예제 #8
0
        public async Task <IActionResult> ChangePassword(ChangePasswordDto changePasswordDto)
        {
            string auth_token = Request.Headers["Authroize"].ToString();
            long   userId     = GetUserIdFromAuthToken(auth_token);

            if (userId == default(long))
            {
                return(RefreshTokenAuthNotVaild());
            }
            var result = await _applicationUserManager.ChangePasswordAsync(_applicationUserManager.FindById(userId), changePasswordDto.CurrentPassword, changePasswordDto.NewPassword);

            if (!result.Succeeded)
            {
                return(RefreshTokenResultFaild(result));
            }
            return(Ok(new ReturnDto()
            {
                Data = null,
                ErrorData = null,
                Status = true
            }));
        }
        public async Task <IActionResult> Index(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetCurrentUserAsync();

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

            var result = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);

            if (result.Succeeded)
            {
                await _userManager.UpdateSecurityStampAsync(user);

                // reflect the changes in the Identity cookie
                await _signInManager.RefreshSignInAsync(user);

                //await _emailSender.SendEmailAsync(
                //           email: user.Email,
                //           subject: "اطلاع رسانی تغییر کلمه‌ی عبور",
                //           viewNameOrPath: "~/Areas/Identity/Views/EmailTemplates/_ChangePasswordNotification.cshtml",
                //           model: new ChangePasswordNotificationViewModel
                //           {
                //               User = user,
                //               EmailSignature = _siteOptions.Value.Smtp.FromName,
                //               MessageDateTime = DateTime.UtcNow.ToLongPersianDateTimeString()
                //           });

                //  return RedirectToAction(nameof(Index), "UserCard", routeValues: new { id = user.Id });

                ViewBag.Sucsess = true;
                return(View(viewName: nameof(Index), model: model));
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }

            return(View(model));
        }
예제 #10
0
        public Claim[] OnVerify(Claim[] claims, JObject payload, string identifier, out bool valid)
        {
            Exception ex;

            valid = false;
            var id    = claims.FirstOrDefault(c => c.Type == identifier).Value;
            var otpId = claims.FirstOrDefault(c => c.Type == "otp_id").Value;
            var hash  = claims.FirstOrDefault(c => c.Type == "otp_hash").Value;

            if (string.Format("{0}:{1}", otpId, payload["otp"].ToString()).Sha256() == hash)
            {
                if (!string.IsNullOrWhiteSpace(payload["password"]?.ToString()))
                {
                    var phone = claims.FirstOrDefault(c => c.Type == "phone").Value;
                    var user  = _applicationUserManager.FindByNameAsync(phone).Result;

                    var result = _applicationUserManager.ChangePasswordAsync(user, payload["password"].ToString()).Result;

                    if (!result.Succeeded)
                    {
                        ex = new Exception();
                        ex.Data.Add(Gp_Error.IdentityResultFaild, result.Errors.ToList());
                        throw ex;
                    }
                }

                valid = true;
                return(new Claim[]
                {
                    new Claim(identifier, id)
                });
            }
            ex = new Exception();
            List <IdentityError> errors = new List <IdentityError>();

            errors.Add(new IdentityError {
                Code        = nameof(ErrorKey.OtpCodeNotValid),
                Description = ErrorKey.OtpCodeNotValid
            });
            ex.Data.Add(Gp_Error.IdentityResultFaild, errors);

            throw ex;
        }
        public async Task <ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var result = await _userManager.ChangePasswordAsync(UserId, model.OldPassword, 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.ChangePasswordSuccess }));
            }
            AddErrors(result);
            return(View(model));
        }
        public async Task <ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var result = await _userManager.ChangePasswordAsync(_userManager.GetCurrentUserId(), model.OldPassword, 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.ChangePasswordSuccess }));
            }
            addErrors(result);
            return(View(model));
        }
예제 #13
0
        public async Task <ApiResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = await _userManager.GetUserAsync(User);

            var result = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);

            if (result.Succeeded)
            {
                await _userManager.UpdateSecurityStampAsync(user);

                // reflect the changes in the Identity cookie
                await _signInManager.RefreshSignInAsync(user);

                await _emailSender.SendEmailAsync(
                    user.Email,
                    "اطلاع رسانی تغییر کلمه‌ی عبور",
                    "~/Views/EmailTemplates/_ChangePasswordNotification.cshtml",
                    new ChangePasswordNotificationViewModel
                {
                    User            = user,
                    EmailSignature  = _siteOptions.Value.Smtp.FromName,
                    MessageDateTime = DateTime.UtcNow.ToLongPersianDateTimeString()
                });

                return(Ok());
            }

            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }

            return(BadRequest());
        }
예제 #14
0
        public async Task <IActionResult> ChangePassword(ChangePasswordViewModel ViewModel)
        {
            var user = await _userManager.GetUserAsync(User);

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

            if (ModelState.IsValid)
            {
                var ChangePassResult = await _userManager.ChangePasswordAsync(user, ViewModel.OldPassword, ViewModel.NewPassword);

                if (ChangePassResult.Succeeded)
                {
                    ViewBag.Alert = "کلمه عبور شما با موفقیت تغییر یافت.";
                }

                else
                {
                    foreach (var item in ChangePassResult.Errors)
                    {
                        ModelState.AddModelError(string.Empty, item.Description);
                    }
                }
            }

            UserSidebarViewModel Sidebar = new UserSidebarViewModel()
            {
                FullName     = user.FirstName + " " + user.LastName,
                LastVisit    = user.LastVisitDateTime,
                RegisterDate = user.RegisterDate,
                Image        = user.Image,
            };

            ViewModel.UserSidebar = Sidebar;
            return(View(ViewModel));
        }
        public async Task <IActionResult> Post([FromBody] ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _usersService.GetCurrentUserAsync();

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

            var result = await _usersService.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);

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

            return(BadRequest());
        }
예제 #16
0
        public async Task <bool> ChangePassword([FromBody] ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(false);
            }

            if (model.NewPassword != model.NewPasswordConfirm)
            {
                return(false);
            }

            ApplicationUser user = await applicationUserManager.FindByIdAsync(User.Identity.GetUserId());

            IdentityResult result = await applicationUserManager.ChangePasswordAsync(user.Id, model.OldPassword, model.NewPassword);

            if (!result.Succeeded)
            {
                return(false);
            }

            return(true);
        }