title: Recommended best practice for role claims as permissions
body: <p>The app I am working on is a SPA and we are using JWT Bearer authentication and OpenIdConnect/OAuth2 when communicating with our backend API which uses .NETCore and ASP.NET Identity.  Our API endpoints are secured using Custom Policy based authentication as shown here: </p>
  
  <p><a href="https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies" rel="noreferrer">Custom Policy Based Authentication</a></p>
  
  <p>We decided to use the out of the box AspNetRoleClaims table to store claims for our users as permissions. Each user is assigned 1 primary role although the potential is there to have multiple roles.  Each role will have many claims - which are stored in the AspNetRoleClaims table.</p>
  
  <p>Role claims would look like this:</p>
  
  <p>ClaimType:  Permission</p>
  
  <p>ClaimValue(s):</p>
  
  <p>MyModule1.Create</p>
  
  <p>MyModule1.Read</p>
  
  <p>MyModule1.Edit</p>
  
  <p>MyModule1.Delete</p>
  
  <p>MyModule1.SomeOtherPermission</p>
  
  <p>MyModule2.Read</p>
  
  <p>MyModule3.Read</p>
  
  <p>MyModule3.Edit</p>
  
  <p>etc.</p>
  
  <p>The more permissions or role claims that a user has, the larger the access_token will be, thereby increasing the HTTP header size.  Also the ASP.NET Identity Authorization cookie - as there are more and more role claims it gets chunked out into multiple cookies.</p>
  
  <p>I have experimented with adding in a lot of role claims and eventually the request fails because the header gets too big.</p>
  
  <p>I am looking for some advice on what is considered "best practice" when it comes to bearer authentication with role claims.  Microsoft gives you AspNetRoleClaims out of the box that work for my scenario and from what I understand the advantage of storing these role claims in the access_token is that we don't have to hit the database on each API endpoint that is secured with the custom policy.</p>
  
  <p>The way I see it, I can try to make the claim values smaller, and in the case of where a user has multiple roles that may share common role claims that are duplicated, I can try to intercept when these get written into the cookie and remove the duplicates.</p>
  
  <p>However, since the app is still in development, I can foresee more and more roles claims being added and there is always the possibility that the HTTP header will become too large with the cookies and the access_token.  Not sure if this is the best approach.</p>
  
  <p>The only alternative I see is to hit the database each time we hit our protected API.  I could inject a DbContext in each custom claim policy requirement handler and talk to the AspNetRoleClaims table on each request. </p>
  
  <p>I haven't seen too many examples out there of how people accomplish a more finely grained permissions scheme with ASP.NET Identity and .NET Core API.  This must be a fairly common requirement I would think...</p>
  
  <p>Anyways, just looking for some feedback and advice on recommended best practice for a scenario like this.</p>
  
  <p>****UPDATE - See answer below ****</p>