You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Identity Server 4中ProfileService.GetProfileDataAsync是否替换默认范围/声明?

IdentityServer4 自定义GetProfileDataAsync后默认声明消失的问题

这是预期行为,不是操作错误。

当你自己实现IProfileService的GetProfileDataAsync方法时,IdentityServer4会完全使用你编写的逻辑生成令牌中的声明,原本自动填充email、profile这类默认范围声明的内置逻辑就不再执行了——默认的声明填充是由IdentityServer自带的DefaultProfileService处理的,一旦你替换成自定义实现,内置的那套逻辑就会被覆盖。

两种解决思路

  • 想保留默认声明同时添加自定义声明:可以在自定义的GetProfileDataAsync中先调用内置DefaultProfileService的同名方法获取基础声明,再补充自定义内容:
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
    // 先调用默认实现获取email、profile这类基础声明
    var defaultService = new DefaultProfileService(_userManager);
    await defaultService.GetProfileDataAsync(context);
    
    // 追加自定义声明
    context.IssuedClaims.Add(new Claim("custom_claim", "your_custom_value"));
}
  • 或者直接从用户存储(比如通过UserManager)查询用户的email、profile相关声明,和自定义声明一起添加到context.IssuedClaims集合中。

总结:这种场景下手动添加默认范围对应的声明是必要的,除非你在自定义实现里复用了内置的默认逻辑。

内容的提问来源于stack exchange,提问作者Bildsoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:12:39