Forgot Password And Reset Password Link On Email In MVC

In this article, we will see how to reset the password through forgot password page in MVC.

Add an Action link on login page if the user forgets his/her password.The link redirects the user to the below page from where the user can get a reset link on the registered email id.

Note: The user should be a already-registered user.

This is how the forgot password page looks .The user needs to enter the registered and valid email id on which the reset link will be sent.
MVC

Below is the code of the forgot password action method

  1. public ActionResult ForgotPassword()
  2. {
  3.     return View();
  4. }
  5. [HttpPost]
  6. public ActionResult ForgotPassword(string UserName)
  7. {
  8.     if (ModelState.IsValid)
  9.     {
  10.         if (WebSecurity.UserExists(UserName))
  11.         {
  12.             string To = UserName, UserID, Password, SMTPPort, Host;
  13.             string token = WebSecurity.GeneratePasswordResetToken(UserName);
  14.             if (token == null)
  15.             {
  16.                 // If user does not exist or is not confirmed.
  17.                 return View("Index");
  18.             }
  19.             else
  20.             {
  21.                 //Create URL with above token
  22.                 var lnkHref = "<a href='" + Url.Action("ResetPassword""Account"new { email = UserName, code = token }, "http") + "'>Reset Password</a>";
  23.                 //HTML Template for Send email
  24.                 string subject = "Your changed password";
  25.                 string body = "<b>Please find the Password Reset Link. </b><br/>" + lnkHref;
  26.                 //Get and set the AppSettings using configuration manager.
  27.                 EmailManager.AppSettings(out UserID, out Password, out SMTPPort, out Host);
  28.                 //Call send email methods.
  29.                 EmailManager.SendEmail(UserID, subject, body, To, UserID, Password, SMTPPort, Host);
  30.             }
  31.         }
  32.     }
  33.     return View();
  34. }

In this article we are using gmail for sending the reset link which requires some settings on the web.config file (the settings are given below the methods).The below method is used to fetch those settings from the web.config file

  1. public class EmailManager {  
  2.     public static void AppSettings(out string UserID, out string Password, out string SMTPPort, out string Host) {  
  3.         UserID = ConfigurationManager.AppSettings.Get("UserID");  
  4.         Password = ConfigurationManager.AppSettings.Get("Password");  
  5.         SMTPPort = ConfigurationManager.AppSettings.Get("SMTPPort");  
  6.         Host = ConfigurationManager.AppSettings.Get("Host");  
  7.     }  
  8.     public static void SendEmail(string From, string Subject, string Body, string To, string UserID, string Password, string SMTPPort, string Host) {  
  9.         System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();  
  10.         mail.To.Add(To);  
  11.         mail.From = new MailAddress(From);  
  12.         mail.Subject = Subject;  
  13.         mail.Body = Body;  
  14.         SmtpClient smtp = new SmtpClient();  
  15.         smtp.Host = Host;  
  16.         smtp.Port = Convert.ToInt16(SMTPPort);  
  17.         smtp.Credentials = new NetworkCredential(UserID, Password);  
  18.         smtp.EnableSsl = true;  
  19.         smtp.Send(mail);  
  20.     }  
  21. }  

Web config settings

MVC

The above code will send a reset link on the registered email.Once the mail is received the user needs to click on the reset link which will redirect the user to the reset password page.

The mail will look like the below image,

Your changed password

Please find the Password Reset Link.

http://localhost:****/Account/ResetPassword?email=**************.com&code=UNbRRYVXWO4mqC15Gfdpaw2

On clicking the above link you will be redirected to a reset password page with the return/ reset token. The return/reset token is attached with the URL of the reset password page and helps in replacing the old password with the new one. User needs to enter the new password in the below page to reset it.

MVC

The code for Resetting password is as below:

  1. public ActionResult ResetPassword(string code, string email)  
  2. {  
  3.     ResetPasswordModel model = new ResetPasswordModel();  
  4.     model.ReturnToken = code;  
  5.     return View(model);  
  6. }  
  7. [HttpPost]     
  8. public ActionResult ResetPassword(ResetPasswordModel model)  
  9. {  
  10.     if (ModelState.IsValid)  
  11.     {  
  12.         bool resetResponse = WebSecurity.ResetPassword(model.ReturnToken, model.Password);  
  13.         if (resetResponse)  
  14.         {  
  15.             ViewBag.Message = "Successfully Changed";  
  16.         }  
  17.         else  
  18.         {  
  19.             ViewBag.Message = "Something went horribly wrong!";  
  20.         }  
  21.     }  
  22.     return View(model);  
  23. }  

The Return token in WebSecurity in Mvc helps in replacing the old password with the new one. The ResetPassword() method in web security is used to reset the password with the help of return token of the registered user.


Similar Articles