IdentityServer v4.1.2客户端授权出现Data is Null错误如何解决
问题排查与解决方案
你遇到的Data is Null报错在IdentityServer4 4.1.2版本中绝大多数由资源、客户端配置缺失导致,可按以下步骤逐一排查修复:
1. 校验身份资源配置
IdentityServer4 4.x版本要求必须显式注册所有用到的身份资源,你请求中携带的openid、profile两个标准scope必须提前配置:
public static IEnumerable<IdentityResource> GetIdentityResources() { return new List<IdentityResource> { new IdentityResources.OpenId(), new IdentityResources.Profile() }; }
同时要在IdentityServer注册逻辑中引入该配置:
services.AddIdentityServer() .AddInMemoryIdentityResources(Config.GetIdentityResources()) // 其余原有配置 ;
2. 校验API范围配置
你请求中携带的自定义scope stock_api必须作为ApiScope显式注册:
public static IEnumerable<ApiScope> GetApiScopes() { return new List<ApiScope> { new ApiScope("stock_api", "股票业务接口访问权限") }; }
同样要在IdentityServer注册逻辑中引入:
services.AddIdentityServer() .AddInMemoryIdentityResources(Config.GetIdentityResources()) .AddInMemoryApiScopes(Config.GetApiScopes()) // 其余原有配置 ;
3. 校验客户端配置
IdentityServer端存储的客户端配置必须和授权请求参数完全匹配,核心校验项如下:
ClientId取值完全等于gyceY9JNAHqincYvSLbD- 授权类型配置为
AllowedGrantTypes = GrantTypes.Code - 跳转地址列表
RedirectUris包含https://localhost:44355/signin-oidc - 允许的scope列表
AllowedScopes同时包含openid、profile、stock_api - 开启PKCE支持:
RequirePkce = true(你的请求携带了PKCE校验参数,服务端必须开启)
4. 校验签名证书配置
调试场景下确认IdentityServer注册逻辑中添加了临时签名证书配置:
services.AddIdentityServer() .AddDeveloperSigningCredential() // 其余原有配置 ;
如果以上配置都确认无误,可开启IdentityServer的Debug级日志,日志会打印空引用的具体触发位置,可快速定位剩余问题。
内容的提问来源于stack exchange,提问作者Shawn
相关产品推荐
相关产品推荐

