在控制器外部访问 HttpContext.Current.User 的新等价物是什么?

What is the new equivalent of accessing HttpContext.Current.User outside of controllers?

我正在编写一个访问当前用户信息的服务。

目前,我通过参数将控制器中的用户 属性 传递到我的逻辑 class 中,并以这种方式使用 ClaimsPrincipal。

我知道通常有一些方法可以全局访问当前用户(例如,在以前的版本中使用 HttpContext.Current.User),那么 MVC 6 中当前的等价物是什么?

The answer to this question 解释说 ClaimsPrincipal.Current 不是线程安全的,因此不是 HttpContext.Current.User

的有用替代品

根据 ,一种方法是使用 DI 将 HttpContextAccessor 注入您的 class,然后您可以通过 _httpContextAccessor.HttpContext.User[= 访问当前用户12=]

我不会详细介绍实施细节,因为他的回答涵盖得很好。

由于该问题一般指的是 HttpContext,而不是针对当前用户,所以我觉得我的问题仍然值得保留以供参考。