This is the exception
An exception of type 'System.InvalidOperationException' occurred in WebMatrix.WebData.dll but was not handled in user code
Additional information: You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.
I m creating an application in which if user clicks on forget password button then he redirects to a page where th email is validated from database and if it is correct it send a mail with reset link and a token to be also generated with the link .
I m getting exception when debugger reaches websecurity line that is boldly displayed here
Is there any solution on how to resolve it or any other way to send link through mail that expires after a limited time and a token to also be generated.
- public ActionResult ForgotPassword( ForgotPasswordViewModel model )
- {
- if (ModelState.IsValid)
- {
- var result = (from x in db.Users
- where x.User_Name == model.User_Name
- select x).FirstOrDefault();
- if (result == null)
- {
- ViewBag.ErrorMessage = "UserName is Incorrect";
- return View("ForgotPassword");
- }
- else
- {
- string to = model.User_Name;
- string token = WebSecurity.GeneratePasswordResetToken(model.User_Name);
- string from = "[email protected]";
- MailMessage message = new MailMessage(from, to);
- string mailbody = "<div>" +
- "<h2>Click on the below link to reset your password</h2>" +
- "</div>" +
- "<div>" +
- " <a href='" + Url.Action("ResetPassword", "Home", new { id = result.User_Id, code = token }, "http") + "'>Reset Password</a>";
- message.Subject = "Reset Password";
- message.Body = mailbody;
- message.BodyEncoding = Encoding.UTF8;
- message.IsBodyHtml = true;
- SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
- System.Net.NetworkCredential basicCredential1 = new
- System.Net.NetworkCredential("[email protected]", "Bholu@1002");
- client.EnableSsl = true;
- client.UseDefaultCredentials = false;
- client.Credentials = basicCredential1;
- try
- {
- client.Send(message);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return View("Login");
- }
- }