【发布时间】:2020-07-19 16:06:19
【问题描述】:
在请求进入任何控制器之前,我需要在中间件中访问当前用户的声明。
登录时我已经设置了这样的声明。
new Claim(ClaimTypes.NameIdentifier, user.ID.ToString());
//...
public async Task InvokeAsync(HttpContext httpContext)
{
var claim = httpContext.User.FindFirst(ClaimTypes.NameIdentifier);
}
它返回 null。
我知道我们可以使用控制器中的ControllerBase.User 属性访问它(我能够获得),但我需要在中间件中访问它。
有什么办法可以做到吗?
我做错了吗?
【问题讨论】:
-
取决于中间件注册顺序。确保您的中间件在身份验证中间件之后注册
-
@Nkosi 你太棒了,谢谢。如果您将此作为答案,我可以接受。谢谢。
标签: c# .net asp.net-core asp.net-core-webapi