Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to User Claims within View Component

Tags:

asp.net-core

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?

like image 942
Mike Anderson Avatar asked Aug 19 '16 12:08

Mike Anderson


People also ask

What is user claim?

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.

What is user claims in ASP.NET Core?

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.

How would you implement claims based authentication in .NET core?

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.


1 Answers

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;
like image 169
adem caglin Avatar answered Oct 19 '22 12:10

adem caglin