title: Custom user authorization based with roles in asp.net mvc
body: <p>I have created a custom authentication and authorisation for my users.The problem I am facing is how to get mvc to check that role from inside my users table matches the [Authorize(Role)] on my controller so as to set httpauthorised to true.Below is my customauthorise class.</p>
  
  <pre><code> [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
  public class CustomAuthorizeAttribute : AuthorizeAttribute 
  {
      public override void OnAuthorization(AuthorizationContext filterContext)
      {
          base.OnAuthorization(filterContext);
          if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
          {
              filterContext.Controller.TempData["ErrorDetails"] = "You must be logged in to access this page";
              filterContext.Result = new RedirectResult("~/User/Login");
              return;
          }
  
          if (filterContext.HttpContext.Request.IsAuthenticated)
          {
              using (var db = new GManagerDBEntities())
              {
                  var authorizedRoles = (from u in db.Users
                                         where u.Username == filterContext.HttpContext.User.Identity.Name
                                         select u.Role).FirstOrDefault();
                  Roles = String.IsNullOrEmpty(Roles) ? authorizedRoles.ToString() : Roles;
              }
          }
  
          if (filterContext.Result is HttpUnauthorizedResult)
          {
              filterContext.Controller.TempData["ErrorDetails"] = "You do nat have necessary rights to access this page";
              filterContext.Result = new RedirectResult("~/User/Login");
              return;
          }
  
      }
      public CustomAuthorizeAttribute(params object[] roles)
      {
          if (roles.Any(r =&gt; r.GetType().BaseType != typeof(Enum)))
              throw new ArgumentException("roles");
  
          this.Roles = string.Join(",", roles.Select(r =&gt; Enum.GetName(r.GetType(), r)));
      }
  }
  </code></pre>
  
  <p>below is my controller with decoration</p>
  
  <pre><code> [CustomAuthorize(Role.Administrator)]
      [HttpGet]
      public ActionResult CreateEmployees()
      {
          return View();
      }
  </code></pre>
  
  <p>and my enum for role</p>
  
  <pre><code>public enum Role
  {
      Administrator = 1,
      UserWithPrivileges = 2,
      User = 3,
  }
  </code></pre>
  
  <p>and model</p>
  
  <pre><code>public class UserModel
  {
      public int UserID { get; set; }
      [Required]
      [Display(Name="Username:")]
      public string Username { get; set; }
      [Required]
      public string Password { get; set; }
      public int Role { get; set; }
  }
  </code></pre>
  
  <p>see pastie for clear view <a href="http://pastie.org/9509756#1,4" rel="nofollow noreferrer">pastie</a></p>
  
  <p>links I have viewed in trying to solve this among others but I cant seem to piece it together<a href="https://stackoverflow.com/questions/9658933/mvc-3-authorize-custom-roles">MVC 3 Authorize custom roles</a>
  <a href="http://forums.asp.net/p/1573254/3948388.aspx" rel="nofollow noreferrer">http://forums.asp.net/p/1573254/3948388.aspx</a></p>
  
  <p><a href="https://stackoverflow.com/questions/20942042/customized-authorization-attribute-in-mvc-4-with-roles">Customized authorization attribute in MVC 4 with Roles</a></p>