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

/AnotherStartup/App_Code/Account/ChangePassword.cs

#
C# | 80 lines | 67 code | 10 blank | 3 comment | 8 complexity | 3f2714b997bd0547887b177b1a45c7c8 MD5 | raw file
  1. using System.Web.WebPages;
  2. using WebMatrix.WebData;
  3. namespace Account
  4. {
  5. /// <summary>
  6. /// Summary description for ChangePassword
  7. /// </summary>
  8. public static class ChangePassword
  9. {
  10. public class Model
  11. {
  12. public string CurrentPassword { get; set; }
  13. public string NewPassword { get; set; }
  14. public string ConfirmPassword { get; set; }
  15. }
  16. public class Error
  17. {
  18. public ErrorMessage CurrentPassword { get; set; }
  19. public ErrorMessage NewPassword { get; set; }
  20. public ErrorMessage ConfirmPassword { get; set; }
  21. public ErrorMessage Provider { get; set; }
  22. public Error()
  23. {
  24. CurrentPassword = ErrorMessage.Nil;
  25. NewPassword = ErrorMessage.Nil;
  26. ConfirmPassword = ErrorMessage.Nil;
  27. Provider = ErrorMessage.Nil;
  28. }
  29. }
  30. public static void Init(WebPageBase web)
  31. {
  32. Model model = web.Page.Model = new Model();
  33. Error error = web.Page.Errors = new Error();
  34. web.Page.Success = false;
  35. model.CurrentPassword = web.Request["currentPassword"];
  36. model.NewPassword = web.Request["newPassword"];
  37. model.ConfirmPassword = web.Request["confirmPassword"];
  38. if (web.IsPost)
  39. {
  40. if (model.CurrentPassword.IsEmpty())
  41. {
  42. error.CurrentPassword = web.Error("Please enter your current password.");
  43. }
  44. if (model.NewPassword.IsEmpty())
  45. {
  46. error.NewPassword = web.Error("Please enter a new password.");
  47. }
  48. if (model.ConfirmPassword.IsEmpty())
  49. {
  50. error.ConfirmPassword = web.Error("Please confirm your new password.");
  51. }
  52. if (model.ConfirmPassword != model.NewPassword)
  53. {
  54. error.ConfirmPassword = web.Error("The password confirmation does not match the new password.");
  55. }
  56. if (web.Page.IsValid)
  57. {
  58. if (WebSecurity.ChangePassword(WebSecurity.CurrentUserName, model.CurrentPassword, model.NewPassword))
  59. {
  60. web.Page.Success = true;
  61. }
  62. else
  63. {
  64. error.Provider = web.Error("An error occurred when attempting to change the password. Please contact the site owner.");
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }