Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net core ControllerBase.User without implementing Controller

Im trying to get an Access token, and to do it I need this line:

 string userId = User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

However, unless I Implement Controller it cannot find User. I've tried HttpContext.User And a few other things as well.

It comes from ControllerBase and looks like public ClaimsPrincipal User { get; }. Is there no way to get this in a normal class with an import?

like image 500
Green_qaue Avatar asked Nov 16 '25 06:11

Green_qaue


1 Answers

You can inject IHttpContextAccessor and access the user like this

_httpContextAccessor.HttpContext.User
like image 168
Marcel Hoekstra Avatar answered Nov 18 '25 18:11

Marcel Hoekstra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!