PageRenderTime 132ms CodeModel.GetById 20ms RepoModel.GetById 2ms app.codeStats 0ms

/Gradebook/Controllers/AccountController.cs

https://bitbucket.org/academium/gradebook
C# | 148 lines | 113 code | 29 blank | 6 comment | 8 complexity | d9640fe3798a96e4a75ec303277adcbd MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Web.Mvc;
  5. using System.Web.Security;
  6. using Gradebook.Contracts.Security;
  7. using Gradebook.Model;
  8. using Gradebook.Models;
  9. using Gradebook.Security.Providers;
  10. namespace Gradebook.Controllers
  11. {
  12. [Authorize]
  13. public class AccountController : Controller
  14. {
  15. public AccountController() { }
  16. public AccountController(IAccountProvider provider) : this(provider, null) { }
  17. public AccountController(IAccountProvider provider, IAuthenticationService service)
  18. {
  19. Provider = provider;
  20. AuthenticationService = service;
  21. }
  22. #region Security
  23. private static IAccountProvider _provider;
  24. private IAuthenticationService _authService;
  25. public static IAccountProvider Provider
  26. {
  27. get { return _provider ?? (_provider = (AccountMembershipProvider)Membership.Provider); }
  28. private set { _provider = value; }
  29. }
  30. private IAuthenticationService AuthenticationService
  31. {
  32. get { return _authService ?? (_authService = new AccountFormsAuthentication()); }
  33. set { _authService = value; }
  34. }
  35. #endregion
  36. #region Membership helpers
  37. public static User CurrentUser
  38. {
  39. get { return Provider.CurrentUser; }
  40. }
  41. public static bool ValidateId(int userId)
  42. {
  43. return CurrentUser.Id == userId;
  44. }
  45. public static bool IsAuthorized
  46. {
  47. get { return Provider.CurrentUser != null; }
  48. }
  49. #endregion
  50. //
  51. // GET: /Account/Login
  52. [AllowAnonymous]
  53. public ActionResult Login(string returnUrl)
  54. {
  55. ViewBag.ReturnUrl = returnUrl;
  56. return View();
  57. }
  58. //
  59. // POST: /Account/Login
  60. [AllowAnonymous]
  61. [HttpPost]
  62. public ActionResult Login(LoginModel model, string returnUrl)
  63. {
  64. if (ModelState.IsValid)
  65. {
  66. if (Provider.ValidateUser(model.UserName, model.Password))
  67. {
  68. AuthenticationService.SetAuthCookie(model.UserName);
  69. if (returnUrl != null && Url.IsLocalUrl(returnUrl))
  70. {
  71. return Redirect(returnUrl);
  72. }
  73. return RedirectToAction("Index", "Home");
  74. }
  75. ModelState.AddModelError("", "The user name or password provided is incorrect.");
  76. }
  77. return View(model);
  78. }
  79. //
  80. // GET: /Account/LogOff
  81. public ActionResult LogOff()
  82. {
  83. AuthenticationService.SignOut();
  84. return RedirectToAction("Index", "Home");
  85. }
  86. private IEnumerable<string> GetErrorsFromModelState()
  87. {
  88. return ModelState.SelectMany(x => x.Value.Errors.Select(error => error.ErrorMessage));
  89. }
  90. #region Status Codes
  91. private static string ErrorCodeToString(MembershipCreateStatus createStatus)
  92. {
  93. switch (createStatus)
  94. {
  95. case MembershipCreateStatus.DuplicateUserName:
  96. return "User name already exists. Please enter a different user name.";
  97. case MembershipCreateStatus.DuplicateEmail:
  98. return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
  99. case MembershipCreateStatus.InvalidPassword:
  100. return "The password provided is invalid. Please enter a valid password value.";
  101. case MembershipCreateStatus.InvalidEmail:
  102. return "The e-mail address provided is invalid. Please check the value and try again.";
  103. case MembershipCreateStatus.InvalidAnswer:
  104. return "The password retrieval answer provided is invalid. Please check the value and try again.";
  105. case MembershipCreateStatus.InvalidQuestion:
  106. return "The password retrieval question provided is invalid. Please check the value and try again.";
  107. case MembershipCreateStatus.InvalidUserName:
  108. return "The user name provided is invalid. Please check the value and try again.";
  109. case MembershipCreateStatus.ProviderError:
  110. return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  111. case MembershipCreateStatus.UserRejected:
  112. return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  113. default:
  114. return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  115. }
  116. }
  117. #endregion
  118. }
  119. }