Getting the ClaimsPrincipal or ClaimsIdentity from inside ASP.NET Core dependency Injection

Viewed 4519

I have a class that has ClaimsIdentity as a dependency like so:

public ClaimsIdentityDecorator(ClaimsIdentity identity)

That is supposed to be the Claimsidentity of the current user. I retrieve it like via the IHttpContextAccessor like so:

services.AddScoped( x=>
{
    var context =  x.GetService<IHttpContextAccessor>();
    return context.HttpContext.User.Identity as ClaimsIdentity;
});

I need to inspect a users claims, and it would seem that ClaimsIdentity or ClaimsPrincipal or some other identity object would be automatically injected for me. Retrieving it from the HttpContext and adding it myself is fine. However, if there is another object or interface that has claims on it that's already there, I would like to design my classes to take that dependency.

Also, I just want to make sure this should be injected at the Scope level.

1 Answers
Related