title: How do I access role objects from HttpContext, or more specifically from a custom authorize attribute?
body: <h2>Background</h2>
  
  <p>I would like to authorize an <code>ApplicationUser</code> based on their <code>ApplicationRole</code>s' <code>RoleClaim</code>s. I would like to implement this like so:</p>
  
  <pre><code>public class RoleClaimAuthorizeAttribute : AuthorizeAttribute
  {
      public RoleClaim RoleClaim { get; set; }
  
      protected override bool AuthorizeCore(HttpContextBase httpContext)
      {
          foreach (ApplicationRole role in Roles)
          {
              if ((RoleClaim &amp; role.Claims) &gt; 0)
              {
                  return true;
              }
          }
          return false;
      }
  }
  </code></pre>
  
  <p>Then I could decorate controller actions like so:</p>
  
  <pre><code>    [RoleClaimAuthorize(RoleClaim = 
          RoleClaim.CanCreateRoles | 
          RoleClaim.CanReadRoles | 
          RoleClaim.CanDeleteRoles | 
          RoleClaim.CanUpdateRoles
      )]
      //
      // GET: /Roles/
      public ActionResult Index()
      {
          return View(_roleManager.Roles);
      }
  </code></pre>
  
  <hr>
  
  <h2>Question</h2>
  
  <p>The problem I'm encountering is any method I can find of reaching an <code>ApplicationUser</code>'s <code>ApplicationRole</code>s from my custom authorize attribute returns a string array of <code>ApplicationRole.Name</code> not an array of <code>ApplicationRole</code> so I can't get to <code>ApplicationRole.Claims</code>. I'm also using Unity instead of Owin to handle <code>ApplicationRoleManager</code> so I can't request <code>ApplicationRoleManager</code> via <code>HttpContext.Current.GetOwinContext().Get&lt;ApplicationRoleManager&gt;()</code>.</p>
  
  <p>So how can I get to a collection of <code>ApplicationRole</code> objects for the current user and thus <code>ApplicationRole.Claims</code>?</p>
  
  <p>Or if it's a more appropriate solution, how can I store a string array of the current <code>ApplicationUser</code>'s <code>ApplicationRole</code>s' <code>RoleClaim</code>s in <code>HttpContext</code> much like how roles are stored? I'm aware my authorization attribute couldn't work as described in this situation but it's a situation I can work with nonetheless.</p>
  
  <hr>
  
  <h2>Relevant Classes</h2>
  
  <h1>ApplicationUser</h1>
  
  <pre><code>// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
  public class ApplicationUser : IdentityUser&lt;Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim&gt;
  {
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      [Key]
      public override Guid Id { get; set; }
  
      public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser, Guid&gt; manager)
      {
          // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
          var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
          // Add custom user claims here
          return userIdentity;
      }
  
      public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser, Guid&gt; manager, string authenticationType)
      {
          // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
          var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
          // Add custom user claims here
          return userIdentity;
      }
  }
  </code></pre>
  
  <h1>ApplicationRole</h1>
  
  <pre><code>public class ApplicationRole : IdentityRole&lt;Guid, ApplicationUserRole&gt;
  {
      public ApplicationRole() : base()
      {
          this.Id = Guid.NewGuid();
      }
  
      public ApplicationRole(string name)
          : this()
      {
          this.Name = name;
      }
  
      public ApplicationRole(string name, params string[] claims)
          : this(name)
      {
          Claims = (RoleClaim)Enum.Parse(typeof(RoleClaim), String.Join(",", claims));
      }
  
      public RoleClaim Claims { get; set; }
  }
  </code></pre>
  
  <h1>RoleClaim</h1>
  
  <pre><code>[Flags]
  public enum RoleClaim : int
  {
      CanCreateUsers = 1,
      CanReadUsers = 2,
      CanUpdateUsers = 4,
      CanDeleteUsers = 8,
      CanCreateRoles = 16,
      CanReadRoles = 32,
      CanUpdateRoles = 64,
      CanDeleteRoles = 128,
      CanCreateTests = 256,
      CanReadTests = 512,
      CanUpdateTests = 1024,
      CanDeleteTests = 2048
  }
  </code></pre>
  
  <h1>ApplicationRoleManager</h1>
  
  <pre><code>public class ApplicationRoleManager : RoleManager&lt;ApplicationRole, Guid&gt;
  {
      public ApplicationRoleManager(IRoleStore&lt;ApplicationRole, Guid&gt; store) : base(store)
      {
      }
  }
  </code></pre>