このコード行を使用したカスタムクレームのマッピングについて、https://leastprivilege.com/2017/11/15/missing-claims-in-the-asp-net-core-2-openid-connect-handler/を読みました。
options.ClaimActions.MapUniqueJsonKey("website", "website");
ロールをマップする必要があり、「ユーザー」のようなロールが1つだけになるまで機能します。
options.ClaimActions.MapUniqueJsonKey("role", "role");
問題は、「User」や「Superadmin」のような複数のロールがある場合です。そのコード行は例外をスローします。
InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JToken.
誰かアイデアがありますか?私は何か間違っていますか、それともバグである可能性がありますか?
ここにこの問題の議論があります:
https://github.com/aspnet/Security/issues/1383
同じ問題で、役割に関する問題の潜在的な解決策は次のとおりです。
https://github.com/aspnet/Security/issues/1383#issuecomment-361505163:
oidcOptions.Events = new OpenIdConnectEvents()
{
OnUserInformationReceived = async context =>
{
// IDS4 returns multiple claim values as JSON arrays, which break the authentication handler
if (context.User.TryGetValue(JwtClaimTypes.Role, out JToken role))
{
var claims = new List<Claim>();
if (role.Type != JTokenType.Array) {
claims.Add(new Claim(JwtClaimTypes.Role, (string)role));
}
else {
foreach (var r in role)
claims.Add(new Claim(JwtClaimTypes.Role, (string)r));
}
var id = context.Principal.Identity as ClaimsIdentity;
id.AddClaims(claims);
}
...
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加