Beispiel #1
0
        public async Task <IActionResult> ForgotPassword(EmailUrlSenderDto model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByEmailAsync(model.Email);

                if (user == null)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, new Response {
                        Status = "Error", Message = "User wasn't found!"
                    }));
                }

                if (!await _userManager.IsEmailConfirmedAsync(user))
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, new Response {
                        Status = "Error", Message = "Email isn't confirmed"
                    }));
                }

                var token = await _userManager.GeneratePasswordResetTokenAsync(user);

                string callbackUrl = _callbackUrlHelper.CreateCallbackUrl(user.Id, token, "ResetPassword");

                try
                {
                    await _emailHelper.SetPasswordSendRuMail(callbackUrl, model);
                }
                catch (Exception exception)
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, new Response {
                        Status = "Error", Message = exception.Message
                    }));
                }
            }

            return(Ok(new Response {
                Status = "Success", Message = "Please, check your email to reset your password."
            }));
        }
Beispiel #2
0
 public async Task SetPasswordSendRuMail(string callbackUrl, EmailUrlSenderDto model)
 {
     string html = $"Please, reset your password <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>here</a>.";
     await _emailSender.SendEmailAsync(model.Email, "Password reset", html);
 }