public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); if (user == null || !(model.BirthYear == user.Birthday.Substring(user.Birthday.Length - 4))) { // Don't reveal that the user does not exist or is not confirmed return(View("ForgotPasswordConfirmation")); } var token = UserManager.GeneratePasswordResetToken(user.Id); string resetPassUrl = Url.Action( controllerName: "Account", actionName: "ResetPassword", routeValues: new { userId = user.Id, token = token }, protocol: Request.Url.Scheme ); EmailMessaging.SendEmail(user.Email, "Forgot Password", "You have requested to change your password. Click <a href=\'" + resetPassUrl + "'>here</a> to reset your password."); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); // var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("ForgotPasswordConfirmation", "Account")); } // If we got this far, something failed, redisplay form return(View(model)); }
public Result <ResetPasswordViewModel> GetResetPasswordModel(long userId) { ResetPasswordViewModel model; Result <ResetPasswordViewModel> result = new Result <ResetPasswordViewModel>(); Result <Users> userResult = new Result <Users>(); userResult = FindById(userId); string code = AppUserManager.GeneratePasswordResetToken(userId); if (userResult.success) { model = new ResetPasswordViewModel(); model.Email = userResult.data.Email; model.UserName = userResult.data.UserName; model.Code = code; result.data = model; } else { result.success = false; result.AddError(userResult.ErrorMessage); } return(result); }
public Result <int> ForgotPassword(ForgotPasswordViewModel model, ControllerBase controllerBase) { Result <int> result = new Result <int>(); Users user = AppUserManager.FindByName(model.UserName); if (user != null && user.Id > 0 && user.IsActive) { ForgotPasswordViewModel info = new ForgotPasswordViewModel(); string code = AppUserManager.GeneratePasswordResetToken(user.Id); var callbackUrl = Common.GetUrlHelper().Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Current.Request.Url.Scheme); info.Name = user.Name; info.Url = callbackUrl; info.UserName = user.UserName; info.Email = user.Email; string defaultPath = "~/Views/Templates/Default/ForgotPassword.cshtml"; string emailBody = Common.RenderRazorViewToString(defaultPath, info, controllerBase); AppUserManager.SendEmail(user.Id, "Reset Password", emailBody); user.IsPasswordResetRequested = true; AppUserManager.Update(user); } else { result.success = false; result.AddError("User does not exist in system"); } return(result); }
private async Task <string> SendForgotPasswordEmail(AppUser user) { var passwordResetToken = AppUserManager.GeneratePasswordResetToken(user.Id); var guiWebsiteBaseUrl = ConfigurationManager.AppSettings[Constants.WebConfig.WebsiteBaseUrlKey]; var guiWebsiteResetPasswordPagePath = ConfigurationManager.AppSettings[Constants.WebConfig.ResetPasswordPagePathKey]; string encodedToken = HttpUtility.UrlEncode(passwordResetToken); var callbackApiUrl = $"{guiWebsiteBaseUrl}{guiWebsiteResetPasswordPagePath}?code={encodedToken}"; try { await AppUserManager.SendEmailAsync(user.Id, Emails.ForgotPasswordTitle, Emails.ForgotPasswordBodyPrefix + callbackApiUrl); } catch { return(await Task.FromResult(Errors.FailedToSendForgotPasswordEmail)); } return(await Task.FromResult(Responses.ForgotPasswordResponseMessage)); }
public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await UserManager.FindByNameAsync(model.EmailAddress); if (user == null) { // Don't reveal that the user does not exist return(RedirectToAction("ResetPasswordConfirmation", "Account")); } var token = UserManager.GeneratePasswordResetToken(user.Id); var result = await UserManager.ResetPasswordAsync(user.Id, token, model.Password); if (result.Succeeded) { return(RedirectToAction("ResetPasswordConfirmation", "Account")); } AddErrors(result); return(View()); }