yash patel

yash patel

  • 1.4k
  • 286
  • 21.4k

I m getting an exception for websecurity

Dec 12 2020 6:20 AM
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.
  1. public ActionResult ForgotPassword( ForgotPasswordViewModel model )  
  2. {  
  3. if (ModelState.IsValid)  
  4. {  
  5. var result = (from x in db.Users  
  6. where x.User_Name == model.User_Name  
  7. select x).FirstOrDefault();  
  8. if (result == null)  
  9. {  
  10. ViewBag.ErrorMessage = "UserName is Incorrect";  
  11. return View("ForgotPassword");  
  12. }  
  13. else  
  14. {  
  15. string to = model.User_Name;  
  16. string token = WebSecurity.GeneratePasswordResetToken(model.User_Name);  
  17. string from = "[email protected]";  
  18. MailMessage message = new MailMessage(from, to);  
  19. string mailbody = "<div>" +  
  20. "<h2>Click on the below link to reset your password</h2>" +  
  21. "</div>" +  
  22. "<div>" +  
  23. " <a href='" + Url.Action("ResetPassword""Home"new { id = result.User_Id, code = token }, "http") + "'>Reset Password</a>";  
  24. message.Subject = "Reset Password";  
  25. message.Body = mailbody;  
  26. message.BodyEncoding = Encoding.UTF8;  
  27. message.IsBodyHtml = true;  
  28. SmtpClient client = new SmtpClient("smtp.gmail.com", 587);  
  29. System.Net.NetworkCredential basicCredential1 = new  
  30. System.Net.NetworkCredential("[email protected]""Bholu@1002");  
  31. client.EnableSsl = true;  
  32. client.UseDefaultCredentials = false;  
  33. client.Credentials = basicCredential1;  
  34. try  
  35. {  
  36. client.Send(message);  
  37. }  
  38. catch (Exception ex)  
  39. {  
  40. throw ex;  
  41. }  
  42. return View("Login");  
  43. }  
  44. }  

Answers (1)