示例#1
0
        public async Task <IActionResult> ForgetPassword(ForgetPasswordDto model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = await _userManager.FindByEmailAsync(model.EmailAddress);

            if (user == null)
            {
                TempData["clientMessage"] =
                    Helper.PrepareClientMessage("Invalid email address.", DataEnum.ClientMessageType.Error);
                return(View(model));
            }

            var token = await _userManager.GeneratePasswordResetTokenAsync(user);

            var message = "Dear: " + model.EmailAddress;

            message += "<br/>Please follow the link below to reset your password<br>";
            message += _configuration["WebsiteUrl"] + "/home/resetpassword?token=" + token + "&email=" + model.EmailAddress;

            var messageParams = new SendMailParamsDto
            {
                ToList = new List <string> {
                    model.EmailAddress
                },
                IsHtml       = true,
                MessageBody  = message,
                MessageTitle = _configuration["WebsiteName"] + ": Reset password"
            };
            var result = await _emailServices.SendMailAsync(messageParams);

            if (result)
            {
                TempData["clientMessage"] =
                    Helper.PrepareClientMessage("Reset password link sent to your email address.",
                                                DataEnum.ClientMessageType.Success);
                return(View());
            }

            TempData["clientMessage"] = Helper.PrepareClientMessage("An error occured while sending the email.",
                                                                    DataEnum.ClientMessageType.Error);

            return(View());
        }