With this code I can get claim from user.Claims
in a Controller
var subClaim = User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
But when try the code in a ViewComponent
the User.Claims
not exits.
Is there any way to get User.Claims
within a ViewComponent
?
A claim is a statement that an entity (a user or another application) makes about itself, it's just a claim. For example a claim list can have the user's name, user's e-mail, user's age, user's authorization for an action. In role-based Security, a user presents the credentials directly to the application.
Claims can be created from any user or identity data which can be issued using a trusted identity provider or ASP.NET Core identity. A claim is a name value pair that represents what the subject is, not what the subject can do.
The claims-based authorization works by checking if the user has a claim to access an URL. In ASP.NET Core we create policies to implement the Claims-Based Authorization. The policy defines what claims that user must process to satisfy the policy. We apply the policy on the Controller, action method, razor page, etc.
Try Request.HttpContext.User.Claims
Also see https://github.com/aspnet/Mvc/issues/4964#issuecomment-230515330
In this github issue there is another solution:
var user = User as ClaimsPrincipal;
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