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

/MvcMusicStore/Controllers/AccountController.cs

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