首页 新闻 会员 周边

在 IdentityServer 中如何给 id token 添加更多信息

0
悬赏园豆:50 [已解决问题] 解决于 2021-02-10 12:56

在我们使用 IdentityServer 的 open api 项目中默认情况下 id token claims 中只包含以下信息

nbf: 1612880007
exp: 1612880307
iss: http://openapi_oauth-server
aud: 61d12f0b-2eb6-4569-ba3e-a9a19fa0deba
nonce: 8006ff68-00c1-4c30-8799-4f07467f42e4
iat: 1612880007
c_hash: eexEqTc_TMc8sc-uLHMH_g
s_hash: jLkR4woiL4ADl2sk5bsyJA
sid: 83583D26E163249D2CD2FBD3FCC6B8FF
sub: 0c7d310b-63cf-dd11-9e4d-001cf0cd1235
auth_time: 1612880007
idp: local
amr: pwd

请问如何增加更多 claim,比如用户名、博客地址?

dudu的主页 dudu | 高人七级 | 园豆:31003
提问于:2021-02-09 22:17
< >
分享
最佳答案
0
收获园豆:50
Timetombs | 老鸟四级 |园豆:3954 | 2021-02-10 09:52

实现 IProfileService 接口可以搞定,但需要注意的是一定要把 Client 的 AlwaysIncludeUserClaimsInIdToken 设置为 true

ProfileService 相关实现代码

public class ProfileService : IProfileService
{
    public async Task GetProfileDataAsync(ProfileDataRequestContext context)
    {
        // ...
        context.IssuedClaims.Add(new Claim(JwtClaimTypes.Name, user.DisplayName));
        context.IssuedClaims.Add(new Claim(JwtClaimTypes.Picture, user.AvatarName));
        context.IssuedClaims.Add(new Claim(JwtClaimTypes.WebSite, user.GetBlogUrl()));
    }
}

Startup 中注册 ProfileService

services.AddIdentityServer(options =>
    {
        // ...
    })
    .AddProfileService<ProfileService>();
dudu | 园豆:31003 (高人七级) | 2021-02-10 12:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册