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.

Below is the code of the forgot password action method
- public ActionResult ForgotPassword()
- {
- return View();
- }
- [HttpPost]
- public ActionResult ForgotPassword(string UserName)
- {
- if (ModelState.IsValid)
- {
- if (WebSecurity.UserExists(UserName))
- {
- string To = UserName, UserID, Password, SMTPPort, Host;
- string token = WebSecurity.GeneratePasswordResetToken(UserName);
- if (token == null)
- {
- // If user does not exist or is not confirmed.
- return View("Index");
- }
- else
- {
- //Create URL with above token
- var lnkHref = "<a href='" + Url.Action("ResetPassword", "Account", new { email = UserName, code = token }, "http") + "'>Reset Password</a>";
- //HTML Template for Send email
- string subject = "Your changed password";
- string body = "<b>Please find the Password Reset Link. </b><br/>" + lnkHref;
- //Get and set the AppSettings using configuration manager.
- EmailManager.AppSettings(out UserID, out Password, out SMTPPort, out Host);
- //Call send email methods.
- EmailManager.SendEmail(UserID, subject, body, To, UserID, Password, SMTPPort, Host);
- }
- }
- }
- return View();
- }
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
- public class EmailManager {
- public static void AppSettings(out string UserID, out string Password, out string SMTPPort, out string Host) {
- UserID = ConfigurationManager.AppSettings.Get("UserID");
- Password = ConfigurationManager.AppSettings.Get("Password");
- SMTPPort = ConfigurationManager.AppSettings.Get("SMTPPort");
- Host = ConfigurationManager.AppSettings.Get("Host");
- }
- public static void SendEmail(string From, string Subject, string Body, string To, string UserID, string Password, string SMTPPort, string Host) {
- System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
- mail.To.Add(To);
- mail.From = new MailAddress(From);
- mail.Subject = Subject;
- mail.Body = Body;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = Host;
- smtp.Port = Convert.ToInt16(SMTPPort);
- smtp.Credentials = new NetworkCredential(UserID, Password);
- smtp.EnableSsl = true;
- smtp.Send(mail);
- }
- }
Web config settings

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.

The code for Resetting password is as below:
- public ActionResult ResetPassword(string code, string email)
- {
- ResetPasswordModel model = new ResetPasswordModel();
- model.ReturnToken = code;
- return View(model);
- }
- [HttpPost]
- public ActionResult ResetPassword(ResetPasswordModel model)
- {
- if (ModelState.IsValid)
- {
- bool resetResponse = WebSecurity.ResetPassword(model.ReturnToken, model.Password);
- if (resetResponse)
- {
- ViewBag.Message = "Successfully Changed";
- }
- else
- {
- ViewBag.Message = "Something went horribly wrong!";
- }
- }
- return View(model);
- }
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.
Smruti LenkaPosted Dec 29, 2022, 12:42 AM
Superb bro ,can you send me code [email protected]
CanhHung888Posted Aug 18, 2022, 4:21 AM
Awesome Bro!! Could you please send code via email [email protected]. Thanks a lot
Atta KumahPosted Nov 29, 2021, 4:11 PM
Great. Bravo
Sonny RascaPosted Jun 7, 2021, 4:25 PM
Hi Aditi! Can you please send me the source code to [email protected] . Many thanks in advance!
Yu-Wen ChenPosted Apr 10, 2021, 8:48 AM
Thanks a lot for your explanation, could you please send me the source code to [email protected]
Abraham EbukaPosted Mar 4, 2021, 11:10 AM
Please does this work with ASP Core 3.1 MVC?
Saberi PradhanPosted Feb 3, 2021, 6:15 PM
Hi, Can you please send the source code to [email protected]. Thanks in advance.
Tharun PrakashPosted Oct 11, 2020, 12:36 AM
Hi, Can you please send the source code to [email protected]. Thanks in advance.
Jay NgoPosted Jul 6, 2020, 7:22 AM
Hello Aditi! Could you please send me the source code to [email protected]!
vedavalli ePosted Jan 13, 2020, 7:44 PM
Hi Aditi, can you please share me source code on [email protected]
noha samirPosted Oct 15, 2019, 2:03 AM
Many thanks for your explanation, could you send please the code at [email protected].
Shivi AttriPosted Aug 19, 2019, 5:22 AM
Hi, Could you please mail me the code at [email protected]
James HamidPosted Aug 15, 2019, 10:09 AM
Hi Could you please email me the source code [email protected]
Dylan ReithelPosted Jul 16, 2019, 4:13 PM
Mind sending me the source code? thanks, great read by the way very informative. [email protected]
emad goharyPosted Jun 23, 2019, 8:42 AM
Hi buddy can you send me email the source code(entire project) this my email address : [email protected]
Aashish JainPosted Jun 14, 2019, 7:20 AM
Hi Aditi Sawhney , Can you please share this code on my mail :- [email protected]
Onur Can ÖzkanPosted Apr 11, 2019, 2:29 AM
Where is ResetPasswordModel coming from ?
Ravin GrewalPosted Mar 30, 2019, 11:28 AM
Hi can u send this Demmy project to mme?
Ahsan AsimPosted Nov 19, 2018, 2:20 AM
Send file on [email protected], Thanks
Ahsan AsimPosted Nov 19, 2018, 2:20 AM
Send file on
Yogesh VedpathakPosted Oct 16, 2018, 9:13 AM
Awesome article very Helpful ...
Hilmi DönmezPosted Sep 6, 2018, 6:28 AM
Hi, could you upload files and share. thank you
Sam BPosted Jun 15, 2018, 1:19 PM
Where is WebSecurity coming from? Why don't you add more details?
hye jinPosted Jun 1, 2018, 8:32 AM
Is there any way to do forgot password page , if I use asp.net core 1.1 ? because on that version , I cannot find the web.config
Sagar Pandurang KapPosted Jan 3, 2018, 12:34 AM
Very nice article...Helpful.