ForgotPassword
How to Implement Forgotten Password that is going to sent the "Url "To reset password by Email
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajeesh MenothRank #118 · 18.4K reputationPosted Jul 29, 2015, 2:09 AM EDT
Vipan SharmaRank #1,589 · 999 reputationPosted Jul 29, 2015, 3:20 AM EDT
Simphiwe Sibiya2 reputationPosted Jul 29, 2015, 1:29 AM EDT
HI, THIS GOES INTO YOUR CONTROLLER
ForgotPassword(ForgotViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(model.Email);
if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
// Don't reveal that the user does not exist or is not confirmed
return View("ForgotPasswordConfirmation");
}
var 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 here: link");
return View("ForgotPasswordConfirmation");
}
return View(model);
}
THIS IS THE VIEW
/// Forgot View
h2>ForgotPassword
@using (Html.BeginForm("ForgotPassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
Enter your email.
@Html.ValidationSummary("", new { @class = "text-danger" })
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
}
Bandisiwe Spiva3 reputationPosted Jul 30, 2015, 1:17 AM EDT
The following code shows the
POST ForgotPasswordmethod.public async TaskActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) { // Don't reveal that the user does not exist or is not confirmed return View("ForgotPasswordConfirmation"); } var 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 here: + callbackUrl + "\">link"); return View("ForgotPasswordConfirmation"); } // If we got this far, something failed, redisplay form return View(model); }