Пример #1
0
        /*
         *
         * public async Task<SignInResult> LoginUser(LoginModel.InputModel model)
         * {
         *    var userName = await _userManager.FindByEmailAsync(model.Email);
         *
         *    if (userName == null)
         *    {
         *        return null;
         *    }
         *
         *    var result = await _signInManager.PasswordSignInAsync(userName, model.Password, model.RememberMe, lockoutOnFailure: true);
         *    if (result.Succeeded)
         *    {
         *        _logger.LogInformation("User logged in.");
         *    }
         *    if (result.IsLockedOut)
         *    {
         *        _logger.LogWarning("User account locked out.");
         *    }
         *
         *    return result;
         * }
         *
         * public async Task<bool> AutoLoginUser(string email, string secret)
         * {
         *    var userName = await _userManager.FindByEmailAsync(email);
         *
         *    if (userName == null)
         *    {
         *        return false;
         *    }
         *
         *    return await _signInManager.ValidateSecurityStampAsync(userName, secret);
         *
         * }*/

        public async Task <bool> ForgotPassword(ForgotPasswordModel.InputModel model, IUrlHelper Url, HttpRequest Request)
        {
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
            {
                return(false);
            }

            // For more information on how to enable account confirmation and password reset please
            // visit https://go.microsoft.com/fwlink/?LinkID=532713
            var code = await _userManager.GeneratePasswordResetTokenAsync(user);

            //TODO
            var callbackUrl = Url.Page(
                "/Account/ResetPassword",
                pageHandler: null,
                values: new { code },
                protocol: Request.Scheme);

            await _emailSender.SendEmailAsync(
                model.Email,
                "Reset Password",
                $"Please reset your password by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

            return(true);
        }
Пример #2
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordModel.InputModel model)
        {
            bool forgot = await _repo.ForgotPassword(model, Url, Request);

            _unitOfWorkManager.Save();

            if (forgot)
            {
                return(Ok());
            }

            return(BadRequest("email is not registered or confirmed"));
        }
Пример #3
0
        public async Task <IActionResult> ForgotPassword([FromServices] IEmailSender _emailSender, ForgotPasswordModel.InputModel ForgotPasswordInput)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByEmailAsync(ForgotPasswordInput.Email);

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

                var code = await _userManager.GeneratePasswordResetTokenAsync(user);

                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                await _emailSender.SendEmailAsync(ForgotPasswordInput.Email, "Reset Password",
                                                  "Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>");

                return(View("ForgotPasswordConfirmation"));
            }

            // If we got this far, something failed, redisplay form
            return(View(ForgotPasswordInput));
        }