【问题标题】:How do you request an Identity Token (id_token) in IdentityServerr4您如何在 IdentityServerr4 中请求身份令牌 (id_token)
【发布时间】:2025-12-05 22:05:02
【问题描述】:

我是 Identity Server 的新手,对身份和访问令牌的主题感到困惑。我了解访问令牌旨在保护资源(即 Web api),并且身份令牌用于进行身份验证。但是,每当我调用 /connect/token 时,我总是会收到一个“access_token”。在请求中,我询问了一个具有各种范围和声明的客户。

new Client
            {             
                ClientId = "Tetris",
                ClientName = "Tetris Web Api",
                AccessTokenLifetime = 60*60*24,
                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                RequireClientSecret = false,
                AllowedScopes = {"openid", "TetrisApi", "TetrisIdentity"}
            }



public static IEnumerable<ApiResource> GetApiResources()
        {
            return new[]
            {
                new ApiResource("TetrisApi", "Tetris Web API", new[] { JwtClaimTypes.Name, JwtClaimTypes.Role, "module" })
            };
        }

        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResource
                {
                    Name = "TetrisIdentity",
                    UserClaims =
                        new[]
                        {
                            JwtClaimTypes.Name,
                            JwtClaimTypes.Role,
                            JwtClaimTypes.GivenName,
                            JwtClaimTypes.FamilyName,
                            JwtClaimTypes.Email,
                            "module",
                            "module.permissions"
                        }
                }
            };
        }

以下是邮递员的副本:

有什么想法吗?我在快速入门中没有看到使用身份令牌的示例。

谢谢!

【问题讨论】:

    标签: identityserver4


    【解决方案1】:

    密码授权类型不支持身份令牌。请参阅 RFC6749。

    您可以在这里做的最好的事情是使用访问令牌通过 userinfo 端点为用户获取声明。

    建议使用隐式或混合等交互式流程进行最终用户身份验证。

    【讨论】:

    • 谢谢。我完全按照你的建议做 - 调用 userinfo 端点。我可以与 Postman 一起使用哪种授权类型来获取身份令牌?
    【解决方案2】:

    @leastprivilege 的答案是正确的,但您也可以选择在 ApiResource 定义中包含您想要的 UserClaims,而不是调用 userinfo 端点。

    目前您请求 new[] { JwtClaimTypes.Name, JwtClaimTypes.Role, "module" },但如果您将其更改为包含您(当前)定义为 IdentityResources 一部分的所有声明,那么这些声明也将在 access_token 中可用。

    【讨论】:

      最近更新 更多