.NET Core Web API : how to access user claims without using the HttpContextAccessor?

Viewed 549

I have a .NET Core 3+ Web API with custom authentication. I refer to it in my startup like so:

services.AddAuthentication(options =>
{
     options.DefaultAuthenticateScheme = CustomAuthOptions.DefaultScheme;
     options.DefaultChallengeScheme = CustomAuthOptions.DefaultScheme;
}).AddCustomAuthentication(options =>
   {
       options.AuthKey = "custom";
   });
   

My custom AuthenticationHandler puts various claims into the Authentication ticket. Code snippets for brevity:

public class CustomAuthHandler : AuthenticationHandler<CustomAuthOptions>
{
    ...
    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        var identity = new ClaimsIdentity("custom");

        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "someUserId"));
        identity.AddClaim(new Claim(ClaimTypes.Name, "someUserName"));

        var identities = new List<ClaimsIdentity> { identity };
        var ticket = new AuthenticationTicket(new ClaimsPrincipal(identities), Options.Scheme);

        return Task.FromResult(AuthenticateResult.Success(ticket));
    }
}

The problem is that I would like to access these claims from the controllers. .NET Core 3 doesn't provide the User or HttpContext anymore. I found that adding the HttpContextAccessor from startup would give access from the controllers.

services.AddHttpContextAccessor();
... 
public TestController(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor) { }

From the injected httpContextAccessor I can get to the claims, but I'm finding this method a bit tedious. I have to inject it in every controller (they all need the claim information). Is there not an easier way I can make these claims available to all my controllers?

Or was it .NET Core's intent to access claims in this way?

0 Answers
Related