IdentityServer4 OpenIdConnect GetClaimsFromUserInfoEndpoint 未设置其他声明

gte*_*dem 5 openid-connect identityserver4

Mvc 混合应用程序不会从 ProfileService 的 id_token 中获取除 name、given_name 和 family_name(我将它们设置在访问令牌中)之外的额外声明。

下面是在ProfileService的GetProfileDataAsync结束时发出的声明

从 GetProfileDataAsync 发出的声明

这是我在 id_token 中得到的内容:

Mvc Id_Token 声明

缺少我在 ProfileService 上设置的图片声明。

所以主要的困惑在这里:

Oidc 选项

1.这不是获取UserInfo端点并与id_token合并吗?如果是这样,这是否按预期工作?

2.如果这按预期工作并且我是否必须在 GetProfileDataAsynch 中设置声明

    if (context.Caller == "ClaimsProviderIdentityToken") //(Or AccessToken)
    {
       //Identity Token claims
    } 
Run Code Online (Sandbox Code Playgroud)

为什么我需要为配置文件范围调用 UserInfo 端点?如果我有很多范围请求,它是否会超过带有大量声明的最大令牌大小?此外,如果我必须将它们全部添加到 id_token (或访问令牌)中,那么将此属性设置为true有何意义?

3.如果我有很多声明并且当我想获取 UserInfo 声明时,我是否需要通过访问令牌调用 UserInfo 端点并自己将它们与身份令牌合并?会不会有超过最大代币大小的问题?或者这只是一个愚蠢的差事?

谢谢你的时间。

GPu*_*uri 2

我也面临同样的问题..用户声明不会自动映射。

首先我的 webapi bff(BackendForFrontEnd) 配置如下所示。身份服务器与电子邮件声明一起发出了声明“角色”。

.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
        {
            options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.RequireHttpsMetadata = false;
            options.ClientId = _identityServerConfiguration.ClientId;
            options.ClientSecret = _identityServerConfiguration.ClientSecret;
            options.ResponseType = _identityServerConfiguration.ResponseType;
            options.UsePkce = _identityServerConfiguration.UsePkce;
            options.Authority = _identityServerConfiguration.AuthorityUrl;
            options.SaveTokens = true;
            var scopesRequested = _identityServerConfiguration.Scope;
            var scopes = scopesRequested.Split(",");
            foreach (var scope in scopes)
            {
                options.Scope.Add(scope);
            }
Run Code Online (Sandbox Code Playgroud)

但是当我尝试访问时经过身份验证

HttpContext.User.FindFirst("role");
HttpContext.User.FindFirst("email");
Run Code Online (Sandbox Code Playgroud)

我的两个值都为 null 然后我在上面指定的 AddOpenIdConnect 方法中添加了以下设置。

options.GetClaimsFromUserInfoEndpoint = true;
Run Code Online (Sandbox Code Playgroud)

之后

HttpContext.User.FindFirst("role"); --> It still gave null
HttpContext.User.FindFirst("email"); --> It worked.
Run Code Online (Sandbox Code Playgroud)

然后我在 AddOpenIdConnect 方法中添加了以下代码

 options.ClaimActions.MapUniqueJsonKey("role", "role");
Run Code Online (Sandbox Code Playgroud)

之后

 HttpContext.User.FindFirst("role"); --> It Worked
 HttpContext.User.FindFirst("email"); --> It worked.
Run Code Online (Sandbox Code Playgroud)

我的最终配置是这样的:

 .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
            {
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.ClaimActions.MapUniqueJsonKey("role", "role");
                options.ClaimActions.MapUniqueJsonKey("grp", "Group");
                options.RequireHttpsMetadata = false;
                options.ClientId = _identityServerConfiguration.ClientId;
                options.ClientSecret = _identityServerConfiguration.ClientSecret;
                options.ResponseType = _identityServerConfiguration.ResponseType;
                options.UsePkce = _identityServerConfiguration.UsePkce;
                options.Authority = _identityServerConfiguration.AuthorityUrl;
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;
                var scopesRequested = _identityServerConfiguration.Scope;
                var scopes = scopesRequested.Split(",");
                foreach (var scope in scopes)
                {
                    options.Scope.Add(scope);
                }
Run Code Online (Sandbox Code Playgroud)

说明: 最初使用 cookie 中的 id_token 来构建 UserPrincipal

然后将 GetClaimsFromUserInfoEndpoint 设置为 true 使 UserPrincipal 也可以使用来自 access_token 的声明。

但它只适用于索赔的姓名和电子邮件类型。最后,MapJsonKey 实际上适用于其余的索赔

这篇文章对此有很好的解释。 https://damienbod.com/2019/11/01/user-claims-in-asp-net-core-using-openid-connect-authentication/comment-page-1/?unapproved=139595&moderation-hash=e41d293e5b41b039a65a776a187c9af7#comment -139595