【发布时间】:2018-08-29 17:53:24
【问题描述】:
我正在尝试在我的 asp.net core 2.0 应用程序中实施自定义授权策略。
在我的自定义 AuthorizationHandler 中我有这个检查:
if (!context.User.Identity.IsAuthenticated)
{
this.logger.LogInformation("Failed to authorize user. User is not authenticated.");
return Task.CompletedTask;
}
// ... More authorization checks
这是有道理的,因为未经身份验证的用户无权执行我的控制器操作。
我正在使用 JWT 不记名身份验证并将其配置如下:
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
};
});
我还在注册我的授权要求和处理程序(请注意,这些注册发生在我在上面配置了身份验证之后,在我调用 serivces.AddMVC() 之前:
services.AddAuthorization(options =>
{
options.AddPolicy(Constants.AuthorizationPolicies.MyCustomPolicy,
policy => policy.Requirements.Add(new MyCustomRequirement()));
});
services.AddScoped<IAuthorizationHandler, MyCustomAuthorizationHandler>();
这是我的问题,我的授权策略似乎在 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 在授权之前执行身份验证?
【问题讨论】:
标签: c# asp.net-core jwt asp.net-authorization