PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/AnotherStartup/App_Code/Account/ForgotPassword.cs

#
C# | 72 lines | 61 code | 7 blank | 4 comment | 7 complexity | 20a23cf778a3e5073b586c757acf2e55 MD5 | raw file
  1. using System;
  2. using System.Web;
  3. using System.Web.Helpers;
  4. using System.Web.WebPages;
  5. using WebMatrix.WebData;
  6. namespace Account
  7. {
  8. /// <summary>
  9. /// Summary description for Confirm
  10. /// </summary>
  11. public static class ForgotPassword
  12. {
  13. public class Model
  14. {
  15. public string Email { get; set; }
  16. }
  17. public class Error
  18. {
  19. public ErrorMessage Email { get; set; }
  20. public Error()
  21. {
  22. Email = ErrorMessage.Nil;
  23. }
  24. }
  25. public static void Init(WebPageBase web)
  26. {
  27. Model model = web.Page.Model = new Model();
  28. Error error = web.Page.Errors = new Error();
  29. web.Page.IsReadonly = false;
  30. model.Email = web.Request.Form["email"] ?? web.Request.QueryString["email"];
  31. if (web.IsPost)
  32. {
  33. var resetToken = string.Empty;
  34. // validate email
  35. if (model.Email.IsEmpty() || !model.Email.Contains("@"))
  36. {
  37. error.Email = web.Error("Please enter a valid email");
  38. }
  39. if (web.Page.IsValid)
  40. {
  41. if (WebSecurity.GetUserId(model.Email) > -1 && WebSecurity.IsConfirmed(model.Email))
  42. {
  43. resetToken = WebSecurity.GeneratePasswordResetToken(model.Email); //Optionally specify an expiration date for the token
  44. }
  45. else
  46. {
  47. web.Page.IsValid = false;
  48. web.Page.IsReadonly = true;
  49. }
  50. }
  51. if (web.Page.IsValid)
  52. {
  53. var hostUrl = web.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
  54. var resetUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/Account/PasswordReset?resetToken=" + HttpUtility.UrlEncode(resetToken));
  55. WebMail.Send(
  56. to: model.Email,
  57. subject: "Please reset your password",
  58. body: "Use this password reset token to reset your password. The token is: " + resetToken + @". Visit <a href=""" + resetUrl + @""">" + resetUrl + "</a> to reset your password."
  59. );
  60. web.Page.IsReadonly = true;
  61. }
  62. }
  63. }
  64. }
  65. }