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.
ClaimsPrincipal exposes a collection of identities, each of which is a ClaimsIdentity. In the common case, this collection, which is accessed through the Identities property, will only have a single element. The introduction of ClaimsPrincipal in .
Adding claims checks Claim based authorization checks are declarative - the developer embeds them within their code, against a controller or an action within a controller, specifying claims which the current user must possess, and optionally the value the claim must hold to access the requested resource.
There's no built in way to provide a ClaimsIdentity
that I'm aware of, but you probably want AddTransient
. If a user logs out, but the instance was created at the beginning of the request AddScoped
won't reflect that change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With