C# asp.net core ControllerBase.User,无需实现控制器

C# asp.net core ControllerBase.User,无需实现控制器,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我正在尝试获取访问令牌,为此,我需要此行: string userId = User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 但是,除非我实现控制器,否则它无法找到用户。我尝试了HttpContext.User和其他一些方法 它来自ControllerBase,看起来像publicsclaimsprincipal用户{get;}。在一个普通的类中,没有办法通过导入来获取

我正在尝试获取访问令牌,为此,我需要此行:

 string userId = User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
但是,除非我实现
控制器
,否则它无法找到
用户
。我尝试了
HttpContext.User
和其他一些方法


它来自
ControllerBase
,看起来像
publicsclaimsprincipal用户{get;}
。在一个普通的类中,没有办法通过导入来获取此信息吗?

您可以注入
IHttpContextAccessor
并像这样访问用户

_httpContextAccessor.HttpContext.User