我在 Angular 应用程序中使用
angular-oauth2-oidc
的代码流。它工作得很好,但我无法阅读用户声明。
我尝试使用
this.oAuthService.getIdToken()
、this.oAuthService.getAccessToken()
、this.oauthService.getUserInfo()
,但我似乎没有获得任何可以使用常规方法解码的有效 JWT。
在我的后端 API (.NET Core) 中,我使用
access_token
来查询 TokenIntrospection 端点,我可以正确地看到所有声明。
相关信息:
export const oAuthConfig: AuthConfig = {
issuer: environment.oauth.issuer,
// URL of the SPA to be redirected to after login
redirectUri: environment.oauth.redirectUri,
clientId: environment.oauth.clientId,
dummyClientSecret: environment.oauth.clientSecret,
responseType: 'code',
scope: 'openid profile email',
showDebugInformation: true,
oidc: true,
requireHttps: false,
};
async login() {
await this.oAuthService.loadDiscoveryDocumentAndTryLogin();
await this.oAuthService.initCodeFlow();
this.router.navigate(['/']);
}
有什么建议吗?感谢您的帮助。
有一个
getIdentityClaims
方法可以为您提供 id_token 声明作为对象。首先,您需要检查 HTTP 请求以验证是否确实返回了 id_token,并且它包含您期望的声明。
发行代币的索赔
在授权服务器中,您可以指定向何处发出声明,并且向两个令牌发出不同的声明是非常标准的:
为了更好地理解这个概念,请参阅 Curity Identity Server 使用的 Claims Mapper 概念。
我怀疑在您的情况下,系统未配置为(或不支持)向 id 令牌发出某些声明。
我的偏好
我实际上认为您的访问令牌处理方式是正确的。 UI 获取用户信息的另一种方法是将访问令牌发送到其 API,路径如 /api/userinfo/current,然后 API 可以返回一个 JSON 对象,其中包含:
此设计有以下好处: