【发布时间】:2021-02-26 17:49:55
【问题描述】:
从 ASP.NET Core 3.1 升级到版本 5 后,context.User.Claims 中为空
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MyRequirement requirement)
在
public class MyRequirementHandler : AuthorizationHandler<MyRequirement>
我正在使用带有 JWT 的不记名令牌的 Authorization 标头。在查看 HttpContext.Request.Headers 时,我可以看到该标头设置正确,但似乎没有被解析。
这是在具有[Authorize] 属性的 Grpc 服务上设置的。
使用 ASP.NET Core 3.1,它运行良好。我查看了official migration guide,但他们关于授权的引用仅适用于 Azure Active Directory。
我正在使用托管在该 ASP.NET Core 应用程序中的 IdentityServer4 作为中间件 (app.UseIdentityServer();)
为了让 ASP.NET Core 正确解析授权标头,我忘了修改什么?
更新:
我更详细地检查了它,并注意到它失败了,因为它无法验证观众 (aud) - 是的,在新创建的令牌上观众丢失了(旧令牌有观众)。我还注意到我正在添加的自定义范围
public override async Task GetProfileDataAsync(ProfileDataRequestContext context)
在我的自定义中
public class ProfileService : ProfileService<ApplicationUser>
更新后也不见了。这是 IdentityServer 的配置方式:
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, AppIdentityDbContext>()
.AddProfileService<ProfileService>()
.AddInMemoryIdentityResources(AuthResources.GetIdentityResources())
.AddInMemoryApiResources(AuthResources.GetApiResources())
.AddInMemoryClients(TestClientsRequired
? ClientsForTesting.GetTestClients()
: Clients.GetDefaultClients());
【问题讨论】:
-
授权令牌的任何部分是否正确解析?这是一个新令牌,还是在迁移之前创建的?
-
它在我的开发环境中,所以它几乎已经创建并且没有解析任何令牌。虽然,我可以使用 jwt.io 验证令牌看起来是否正确。除了配置值之外,用户对象非常空,所以我假设没有任何内容被解析
标签: asp.net-core identityserver4 jwt-auth asp.net-authorization asp.net-core-5.0