/Gradebook/Controllers/AccountController.cs
C# | 148 lines | 113 code | 29 blank | 6 comment | 8 complexity | d9640fe3798a96e4a75ec303277adcbd MD5 | raw file
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Web.Mvc;
- using System.Web.Security;
- using Gradebook.Contracts.Security;
- using Gradebook.Model;
- using Gradebook.Models;
- using Gradebook.Security.Providers;
-
- namespace Gradebook.Controllers
- {
- [Authorize]
- public class AccountController : Controller
- {
- public AccountController() { }
- public AccountController(IAccountProvider provider) : this(provider, null) { }
- public AccountController(IAccountProvider provider, IAuthenticationService service)
- {
- Provider = provider;
- AuthenticationService = service;
- }
-
- #region Security
-
- private static IAccountProvider _provider;
- private IAuthenticationService _authService;
-
- public static IAccountProvider Provider
- {
- get { return _provider ?? (_provider = (AccountMembershipProvider)Membership.Provider); }
- private set { _provider = value; }
- }
- private IAuthenticationService AuthenticationService
- {
- get { return _authService ?? (_authService = new AccountFormsAuthentication()); }
- set { _authService = value; }
- }
-
- #endregion
-
- #region Membership helpers
-
- public static User CurrentUser
- {
- get { return Provider.CurrentUser; }
- }
-
- public static bool ValidateId(int userId)
- {
- return CurrentUser.Id == userId;
- }
-
- public static bool IsAuthorized
- {
- get { return Provider.CurrentUser != null; }
- }
-
- #endregion
-
- //
- // GET: /Account/Login
-
- [AllowAnonymous]
- public ActionResult Login(string returnUrl)
- {
- ViewBag.ReturnUrl = returnUrl;
- return View();
- }
-
- //
- // POST: /Account/Login
-
- [AllowAnonymous]
- [HttpPost]
- public ActionResult Login(LoginModel model, string returnUrl)
- {
- if (ModelState.IsValid)
- {
- if (Provider.ValidateUser(model.UserName, model.Password))
- {
- AuthenticationService.SetAuthCookie(model.UserName);
- if (returnUrl != null && Url.IsLocalUrl(returnUrl))
- {
- return Redirect(returnUrl);
- }
- return RedirectToAction("Index", "Home");
- }
- ModelState.AddModelError("", "The user name or password provided is incorrect.");
- }
-
- return View(model);
- }
-
- //
- // GET: /Account/LogOff
-
- public ActionResult LogOff()
- {
- AuthenticationService.SignOut();
-
- return RedirectToAction("Index", "Home");
- }
-
- private IEnumerable<string> GetErrorsFromModelState()
- {
- return ModelState.SelectMany(x => x.Value.Errors.Select(error => error.ErrorMessage));
- }
-
- #region Status Codes
- private static string ErrorCodeToString(MembershipCreateStatus createStatus)
- {
- switch (createStatus)
- {
- case MembershipCreateStatus.DuplicateUserName:
- return "User name already exists. Please enter a different user name.";
-
- case MembershipCreateStatus.DuplicateEmail:
- return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
-
- case MembershipCreateStatus.InvalidPassword:
- return "The password provided is invalid. Please enter a valid password value.";
-
- case MembershipCreateStatus.InvalidEmail:
- return "The e-mail address provided is invalid. Please check the value and try again.";
-
- case MembershipCreateStatus.InvalidAnswer:
- return "The password retrieval answer provided is invalid. Please check the value and try again.";
-
- case MembershipCreateStatus.InvalidQuestion:
- return "The password retrieval question provided is invalid. Please check the value and try again.";
-
- case MembershipCreateStatus.InvalidUserName:
- return "The user name provided is invalid. Please check the value and try again.";
-
- case MembershipCreateStatus.ProviderError:
- return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
-
- case MembershipCreateStatus.UserRejected:
- return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
-
- default:
- return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
- }
- }
- #endregion
- }
- }