简体   繁体   English

如何在 Middleware.Net Core Web API 中获取当前用户/声明主体

[英]How to get current User/ClaimsPrincipal in Middleware .Net Core Web API

I need to access Current User's claims in Middleware before request goes any Controllers.在请求进入任何控制器之前,我需要在中间件中访问当前用户的声明。

On login I have set claims like this.在登录时,我已经设置了这样的声明。

new Claim(ClaimTypes.NameIdentifier, user.ID.ToString()),

    public async Task InvokeAsync(HttpContext httpContext)
        {
           var claim = httpContext.User.FindFirst(ClaimTypes.NameIdentifier)
        }

It returns null.它返回 null。

I know that we can access this using ControllerBase.User property in Controllers(which I am able to get), But I need to access it in Middleware.我知道我们可以使用 Controllers 中的 ControllerBase.User 属性访问它(我能够获得),但我需要在中间件中访问它。

Is there any way to achieve this?有什么办法可以做到这一点?

Am I doing something wrong?难道我做错了什么?

The reason you can access this using ControllerBase.User property in Controllers is because it happens later in the pipeline well after the User has been set.您可以使用ControllerBase.User属性访问它的原因是,它会在设置User之后的管道中稍后发生。

This depends on middleware registration order.这取决于中间件注册顺序。

The order that middleware components are added in the Startup.Configure method defines the order in which the middleware components are invoked on requests and the reverse order for the response.Startup.Configure方法中添加中间件组件的顺序定义了对请求调用中间件组件的顺序和响应的相反顺序。 The order is critical for security, performance, and functionality.顺序对于安全性、性能和功能至关重要。

The User is set by the authentication middleware(s) so make sure your custom middleware that depend on having User is registered after the authentication middleware(s) User由身份验证中间件设置,因此请确保在身份验证中间件之后注册依赖于User的自定义中间件

//...

app.UseAuthentication();
app.UseAuthorization();
// app.UseSession();

app.AddCustomMiddleware();

//...

Reference ASP.NET Core Middleware参考ASP.NET 核心中间件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET CORE,如何在中间件中获取 Web 应用程序中异常的文件位置? - .NET CORE, how to get file location for an exception in a web app, in middleware? ASP.NET 核心 Web API - 如何在中间件管道中隐藏 DbContext 事务? - ASP.NET Core Web API - How to hide DbContext transaction in the middleware pipeline? .NET Core 2.2 Web API Azure门户AD身份验证| 如何使用有效的Bearer令牌获取用户信息? - .NET Core 2.2 Web API | Azure Portal AD Authentication | How to get user info with a valid Bearer Token? 如何在Asp .Net core 2.1中获取Ubuntu系统的当前用户? - How to get the current user of the Ubuntu system in Asp .Net core 2.1? NET Core Web API中如何使用HTTP get方法传递参数? - how to pass a argument with HTTP get method in .net core web api? 如何从外部网站获取数据到.NET Core中的Web API? - How to get data from external website into a web API in .NET core? 如何获取核心获取当前用户信息? - How get core get current user info? c# .net Web Api Windows 和匿名身份验证获取当前用户 - c# .net Web Api Windows and Anonymous Authentication get current user .NET 核心集 ClaimsPrincipal 来自另一个 controller - .NET Core Set ClaimsPrincipal from another controller 用户身份验证之前的初始 API 令牌 - .NET 核心 Web API - Initial API token before user authentication - .NET core Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM