【发布时间】:2020-01-28 22:32:21
【问题描述】:
我编写了一个小的中间件代码(asp.net core v2.2 + c#),它在执行对服务器的调用之后运行,如果用户通过身份验证,则运行一些逻辑。由于它是 WebAPI - 身份验证是通过使用 Bearer 令牌完成的。
中间件是这样的:
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
await _next(httpContext).ConfigureAwait(false); // calling next middleware
if (httpContext.User.Identity.IsAuthenticated) // <==================== Allways false
{
// Do my logics
}
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class MyMiddlewareExtensions
{
public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<MyMiddleware>();
}
}
问题在于表达式httpContext.User.Identity.IsAuthenticated 总是返回false,即使请求成功通过服务验证。
我的Startup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
app.UseAuthentication();
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
// Formatting numbers, dates, etc.
SupportedCultures = new[] { new CultureInfo("en-US") },
// UI strings that we have localized.
SupportedUICultures = supportedCultures,
});
app.UseMvc();
app.UseMyMiddleware(ConfigurationManager.ApplicationName);
}
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddAuthentication().AddJwtBearer(options =>
{
// ...
});
}
我还检查了httpContext.Request 对象是否包含Authorization 标头,并且确实如此。
为什么httpContext.User 对象似乎请求未经授权?
【问题讨论】:
-
试试
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(...) -
您好,感谢您的回答。结果相同。
-
如果我没记错的话,IsAuthenticated 属性会检查是否存在“名称”声明(您也可以更改 JWT 设置中的名称声明。它不是基于身份验证是否成功.
-
啊不,不是名称声明,而是身份验证类型:github.com/microsoft/referencesource/blob/master/mscorlib/…
-
你的中间件是在 MVC 之后注册的,所以只有在请求不匹配任何操作时才会运行,这是故意的吗?
标签: c# asp.net asp.net-core asp.net-web-api