ASP.NET Core 3.1에서 ASP.NET 5.0으로 업그레이드 한 후 User.Claims가 비어 있음
버전 5 ASP.NET 코어 3.1에서 업그레이드 한 후, context.User.Claims
빈에있다
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MyRequirement requirement)
에
public class MyRequirementHandler : AuthorizationHandler<MyRequirement>
Authorization
JWT와 함께 전달자 토큰이 있는 헤더를 사용하고 있습니다. 를 볼 때 헤더가 올바르게 설정된 것을 볼 수 HttpContext.Request.Headers
있지만 구문 분석되지 않은 것 같습니다.
이것은 [Authorize]
속성 을 사용하여 Grpc 서비스에 설정됩니다 .
ASP.NET Core 3.1에서는 제대로 작동했습니다. 공식 마이그레이션 가이드 를 살펴 보았지만 권한 부여에 대한 참조는 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());
답변
문제가 청중 누락으로 인한 것일 수 있음을 파악한 후 ( aud
) 자세히 살펴본 결과 액세스 토큰에서 누락 된 "aud"클레임을 발견 했습니다. 대답은 청중을 명시 적으로 클레임으로 추가하고 범위를 한 번 더 설정하는 것이 었습니다. , 그리고 작동했습니다.
나를 위해 이것은 다음과 같은 방식으로 보입니다.
public static IEnumerable<ApiResource> GetApiResources()
{
yield return ApiResourceBuilder
.IdentityServerJwt(MyWebApiResource)
.AllowAllClients()
.Build()
.AddUserClaims()
.AddScopes(); // <- this is new
}
private static T AddUserClaims<T>(this T resource)
where T : Resource
{
resource.UserClaims.Add(Constants.CustomClaimTypes.MyRoles);
resource.UserClaims.Add(JwtClaimTypes.Audience); // <- this is new
return resource;
}
// this whole method is new ->
private static T AddScopes<T>(this T resource)
where T : ApiResource
{
resource.Scopes.Add(MyWebApiResource);
return resource;
}