title: Cannot access HttpContextAccessor User Claims on OnActionExecuting
body: <p>I'm using .net core with JWT for authentication. And I'm trying to access user claims from <code>HttpContextAccessor</code> in my custom attribute. From within the app, I have a <code>UserService.cs</code> (code below) where I can do this. But on <code>OnActionExecuting</code> in my custom attribute, the claims come as an empty error. Even if I call the function from my user service, the claims aren't there.</p>
  <p>My ultimate objective is to get the user's id to check if the user has admin access. I don't wanna store the admin access status on the token.</p>
  <p><a href="https://i.stack.imgur.com/kFrbI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kFrbI.png" alt="enter image description here" /></a></p>
  <p><strong>UserService.cs</strong></p>
  <pre class="lang-cs prettyprint-override"><code>
      public AuthenticatedUserClaims AuthenticatedUser()
              {
                  var userClaims = new AuthenticatedUserClaims();
                  var claims = _contextAccessor.HttpContext.User.Claims;
                  var enumerable = claims as Claim[] ?? claims.ToArray();
                  var userId = enumerable.SingleOrDefault(x =&gt; x.Type == &quot;UserId&quot;)?.Value;
                  userClaims.UserName = enumerable.SingleOrDefault(x =&gt; x.Type == &quot;UserName&quot;)?.Value;
                  userClaims.FullName = enumerable.SingleOrDefault(x =&gt; x.Type == &quot;FullName&quot;)?.Value;
                  if (userId != null &amp;&amp; !string.IsNullOrEmpty(userId)) userClaims.UserId = int.Parse(userId);
      
                  return userClaims;
              }
  </code></pre>
  <p><strong>My Custom Attribute</strong></p>
  <pre class="lang-cs prettyprint-override"><code>        [AttributeUsage(AttributeTargets.Class| AttributeTargets.Method)]
          public class PermissionsRequiredAttribute: ActionFilterAttribute
          {
              private readonly IHttpContextAccessor _contextAccessor;
      
              public PermissionsRequiredAttribute(IHttpContextAccessor contextAccessor)
              {
                  _contextAccessor = contextAccessor;
              }
              public override void OnActionExecuting(ActionExecutingContext context)
              {
                  var claims = _contextAccessor.HttpContext.User.Claims;
                  var claimsList = claims as Claim[] ?? claims.ToArray();
                  // claimsList = Claims[0]??
                  // context.Result = new UnauthorizedResult();
                  base.OnActionExecuting(context);
              }
              
          }
  </code></pre>
  <p><strong>Attribute Usage</strong></p>
  <pre class="lang-cs prettyprint-override"><code>     [HttpGet(&quot;{id}&quot;)]
       [ServiceFilter(typeof(PermissionsRequiredAttribute))]
       public async Task&lt;ActionResult&lt;Beneficiary&gt;&gt; GetBeneficiary([FromRoute] int id) { //... }
  </code></pre>
  <p><strong>ConfigureServices on Startup.cs</strong></p>
  <pre class="lang-cs prettyprint-override"><code>    services.AddTransient&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
      services.AddScoped&lt;PermissionsRequiredAttribute&gt;();
  </code></pre>
  <p>Thanks in advance :)</p>