Exemplo n.º 1
0
        public async Task <ActionResult> ForgotPassword(ForgotPassword formModel)
        {
            var user = await _signInManager.UserManager.FindByEmailAsync(formModel.Email);

            if (user != null)
            {
                var token = await _signInManager.UserManager.GeneratePasswordResetTokenAsync(user);

                var callbackUrl = Url.Action("ResetPassword", "Account", new { UserId = user.Id, Token = token }, protocol: Request.Scheme);

                var resetPasswordEmailNotification = new ResetPasswordEmailNotification(WorkContext.CurrentStore.Id, WorkContext.CurrentLanguage)
                {
                    Url       = callbackUrl,
                    Sender    = WorkContext.CurrentStore.Email,
                    Recipient = GetUserEmail(user)
                };

                var sendingResult = await _platformNotificationApi.SendNotificationAsync(resetPasswordEmailNotification.ToNotificationDto());

                if (sendingResult.IsSuccess != true)
                {
                    ModelState.AddModelError("form", sendingResult.ErrorMessage);
                }
            }
            else
            {
                ModelState.AddModelError("form", "User not found");
            }

            return(View("customers/forgot_password", WorkContext));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> ForgotPassword(ForgotPassword formModel)
        {
            TryValidateModel(formModel);
            if (!ModelState.IsValid)
            {
                return(View("customers/forgot_password", WorkContext));
            }

            var user = await _signInManager.UserManager.FindByEmailAsync(formModel.Email);

            if (user == null)
            {
                user = await _signInManager.UserManager.FindByNameAsync(formModel.Email);
            }

            if (user == null)
            {
                WorkContext.Form.Errors.Add(SecurityErrorDescriber.OperationFailed());
                return(View("customers/forgot_password", WorkContext));
            }

            var successViewName = "customers/forgot_password";
            NotificationBase resetPasswordNotification = null;

            if (_options.ResetPasswordNotificationGateway.EqualsInvariant("Phone"))
            {
                successViewName = "customers/forgot_password_code";
                var phoneNumber = await _signInManager.UserManager.GetPhoneNumberAsync(user);

                if (string.IsNullOrEmpty(phoneNumber))
                {
                    WorkContext.Form.Errors.Add(SecurityErrorDescriber.PhoneNumberNotFound());
                    return(View("customers/forgot_password", WorkContext));
                }

                var token = await _signInManager.UserManager.GenerateUserTokenAsync(user, TokenOptions.DefaultPhoneProvider, "ResetPassword");

                resetPasswordNotification = new ResetPasswordSmsNotification(WorkContext.CurrentStore.Id, WorkContext.CurrentLanguage)
                {
                    Token     = token,
                    Recipient = phoneNumber,
                };

                // This required for populate hidden fields on the form
                WorkContext.Form = Form.FromObject(new ResetPasswordByCodeModel
                {
                    Email = user.Email
                });
            }
            else // "Email"
            {
                var token = await _signInManager.UserManager.GeneratePasswordResetTokenAsync(user);

                var callbackUrl = Url.Action("ResetPassword", "Account", new { UserId = user.Id, Token = token }, protocol: Request.Scheme, host: WorkContext.CurrentStore.Host);

                resetPasswordNotification = new ResetPasswordEmailNotification(WorkContext.CurrentStore.Id, WorkContext.CurrentLanguage)
                {
                    Url       = callbackUrl,
                    Sender    = WorkContext.CurrentStore.Email,
                    Recipient = GetUserEmail(user)
                };
            }

            var sendingResult = await SendNotificationAsync(resetPasswordNotification);

            if (sendingResult.IsSuccess == true)
            {
                return(View(successViewName, WorkContext));
            }

            WorkContext.Form.Errors.Add(SecurityErrorDescriber.ErrorSendNotification(sendingResult.ErrorMessage));
            return(View("customers/forgot_password", WorkContext));
        }