/Sample.Mvc/Controllers/AccountController.cs

https://github.com/robertmilne/MVC-Mini-Profiler · C# · 193 lines · 135 code · 34 blank · 24 comment · 13 complexity · c6c3ff8c883b4c540a4d0b069a36276d MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7. using System.Web.Security;
  8. using SampleWeb.Models;
  9. namespace SampleWeb.Controllers
  10. {
  11. public class AccountController : BaseController
  12. {
  13. //
  14. // GET: /Account/LogOn
  15. public ActionResult LogOn()
  16. {
  17. return View();
  18. }
  19. //
  20. // POST: /Account/LogOn
  21. [HttpPost]
  22. public ActionResult LogOn(LogOnModel model, string returnUrl)
  23. {
  24. if (ModelState.IsValid)
  25. {
  26. if (Membership.ValidateUser(model.UserName, model.Password))
  27. {
  28. FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
  29. if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
  30. && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
  31. {
  32. return Redirect(returnUrl);
  33. }
  34. else
  35. {
  36. return RedirectToAction("Index", "Home");
  37. }
  38. }
  39. else
  40. {
  41. ModelState.AddModelError("", "The user name or password provided is incorrect.");
  42. }
  43. }
  44. // If we got this far, something failed, redisplay form
  45. return View(model);
  46. }
  47. //
  48. // GET: /Account/LogOff
  49. public ActionResult LogOff()
  50. {
  51. FormsAuthentication.SignOut();
  52. return RedirectToAction("Index", "Home");
  53. }
  54. //
  55. // GET: /Account/Register
  56. public ActionResult Register()
  57. {
  58. return View();
  59. }
  60. //
  61. // POST: /Account/Register
  62. [HttpPost]
  63. public ActionResult Register(RegisterModel model)
  64. {
  65. if (ModelState.IsValid)
  66. {
  67. // Attempt to register the user
  68. MembershipCreateStatus createStatus;
  69. Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
  70. if (createStatus == MembershipCreateStatus.Success)
  71. {
  72. FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
  73. return RedirectToAction("Index", "Home");
  74. }
  75. else
  76. {
  77. ModelState.AddModelError("", ErrorCodeToString(createStatus));
  78. }
  79. }
  80. // If we got this far, something failed, redisplay form
  81. return View(model);
  82. }
  83. //
  84. // GET: /Account/ChangePassword
  85. [Authorize]
  86. public ActionResult ChangePassword()
  87. {
  88. return View();
  89. }
  90. //
  91. // POST: /Account/ChangePassword
  92. [Authorize]
  93. [HttpPost]
  94. public ActionResult ChangePassword(ChangePasswordModel model)
  95. {
  96. if (ModelState.IsValid)
  97. {
  98. // ChangePassword will throw an exception rather
  99. // than return false in certain failure scenarios.
  100. bool changePasswordSucceeded;
  101. try
  102. {
  103. MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
  104. changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
  105. }
  106. catch (Exception)
  107. {
  108. changePasswordSucceeded = false;
  109. }
  110. if (changePasswordSucceeded)
  111. {
  112. return RedirectToAction("ChangePasswordSuccess");
  113. }
  114. else
  115. {
  116. ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
  117. }
  118. }
  119. // If we got this far, something failed, redisplay form
  120. return View(model);
  121. }
  122. //
  123. // GET: /Account/ChangePasswordSuccess
  124. public ActionResult ChangePasswordSuccess()
  125. {
  126. return View();
  127. }
  128. #region Status Codes
  129. private static string ErrorCodeToString(MembershipCreateStatus createStatus)
  130. {
  131. // See http://go.microsoft.com/fwlink/?LinkID=177550 for
  132. // a full list of status codes.
  133. switch (createStatus)
  134. {
  135. case MembershipCreateStatus.DuplicateUserName:
  136. return "User name already exists. Please enter a different user name.";
  137. case MembershipCreateStatus.DuplicateEmail:
  138. return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
  139. case MembershipCreateStatus.InvalidPassword:
  140. return "The password provided is invalid. Please enter a valid password value.";
  141. case MembershipCreateStatus.InvalidEmail:
  142. return "The e-mail address provided is invalid. Please check the value and try again.";
  143. case MembershipCreateStatus.InvalidAnswer:
  144. return "The password retrieval answer provided is invalid. Please check the value and try again.";
  145. case MembershipCreateStatus.InvalidQuestion:
  146. return "The password retrieval question provided is invalid. Please check the value and try again.";
  147. case MembershipCreateStatus.InvalidUserName:
  148. return "The user name provided is invalid. Please check the value and try again.";
  149. case MembershipCreateStatus.ProviderError:
  150. return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  151. case MembershipCreateStatus.UserRejected:
  152. return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  153. default:
  154. return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  155. }
  156. }
  157. #endregion
  158. }
  159. }