C# asp.net core 2.0授权在身份验证之前启动(JWT)
我正在尝试在asp.net core 2.0应用程序中实现自定义授权策略 在我的自定义授权处理程序中,我有以下检查:C# asp.net core 2.0授权在身份验证之前启动(JWT),c#,asp.net-core,jwt,asp.net-authorization,C#,Asp.net Core,Jwt,Asp.net Authorization,我正在尝试在asp.net core 2.0应用程序中实现自定义授权策略 在我的自定义授权处理程序中,我有以下检查: if (!context.User.Identity.IsAuthenticated) { this.logger.LogInformation("Failed to authorize user. User is not authenticated."); return Task.CompletedTask; } // ... More author
if (!context.User.Identity.IsAuthenticated)
{
this.logger.LogInformation("Failed to authorize user. User is not authenticated.");
return Task.CompletedTask;
}
// ... More authorization checks
这是有道理的,因为未经验证的用户无权执行我的控制器操作
我正在使用JWT Bear auth,并将其配置为:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "<issuer>",
ValidAudience = "<audience>",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(signingKey)),
ValidateLifetime = false
};
});
这是我的问题,我的授权策略似乎是在jwt令牌被质询和验证之前执行的,因此,我的授权策略失败,因为用户没有经过身份验证
下面是一个示例调用的日志:
Now listening on: http://localhost:60235
Application started. Press Ctrl+C to shut down.
[14:50:57 INF] Request starting HTTP/1.1 GET http://localhost:60235/api/values application/json
[14:51:03 INF] Failed to authorize user. User is not authenticated.
[14:51:03 INF] Authorization failed for user: null.
[14:51:03 INF] Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
[14:51:03 INF] Executing ChallengeResult with authentication schemes ([]).
[14:51:03 INF] Successfully validated the token.
[14:51:03 INF] AuthenticationScheme: Bearer was challenged.
[14:51:03 INF] Executed action AuthorizationTestClient.Controllers.ValuesController.Get (AuthorizationTestClient) in 5819.1586ms
[14:51:03 INF] Request finished in 5961.5619ms 401
如您所见,授权策略首先执行,失败是因为用户未经过身份验证,然后发生身份验证质询
这不应该是另一种方式吗?如何告诉asp.net在授权之前执行身份验证
我还注册了我的授权要求和处理程序(注
这些注册发生在我配置了上面的身份验证之后,
在调用serivces.AddMVC()之前
DI容器中服务注册的顺序不太重要。重要的是启动中中间件注册的顺序。配置方法。您没有提供此方法的代码,但我打赌您会在MVC中间件之后添加身份验证中间件,或者根本不添加它。身份验证middleware应该在MVC之前添加,因此请确保您的启动。Configure
看起来与此类似:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMvc();
}
查看以下文章了解更多详细信息:
好的..原来这很容易修复。我需要在我的配置方法中调用app.UseAuthentication()。我真傻
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMvc();
}
你读过这篇文章吗?那么,你为什么要发布与我发布的答案相同的答案?我同意,你应该删除你的答案并接受@CodeFuller的答案我实际上在CodeFuller发布他的答案之前4分钟发布了我的回复。将鼠标悬停在用户名上方的“已回答…”标签上。我的答案是“2018-03-20 19:20:27Z”,你的答案是“2018-03-20 19:24:00Z”,这是4分钟后。按最旧选项卡排序答案也会显示正确的顺序。只需接受CodeFuller的答案,不要重复以前的答案。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMvc();
}