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
相关产品推荐
相关产品推荐

