首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >angular-oauth2-oidc内标识在登录后始终无效

angular-oauth2-oidc内标识在登录后始终无效
EN

Stack Overflow用户
提问于 2020-05-08 18:19:07
回答 2查看 2.4K关注 0票数 0

我正在尝试遵循documentation https://github.com/manfredsteyer/angular-oauth2-oidc上的指南。

我在服务的构造函数中有以下配置:

代码语言:javascript
复制
authCodeFlowConfig: AuthConfig = {
   issuer: 'https://demo.identityserver.io',
   redirectUri: window.location.origin + '/home',
   clientId: 'spa',
   responseType: 'code',
   scope: 'openid profile email offline_access api',
   showDebugInformation: true
 };

this.oauthService.configure(this.authCodeFlowConfig);

(我也尝试过index.html的重定向url,但似乎没有什么不同)

登录似乎工作得很好,我得到了重定向到页面的登录,并被重定向到主页:

代码语言:javascript
复制
    this.oauthService.loadDiscoveryDocumentAndTryLogin().then((response) => {
      if (!this.oauthService.hasValidIdToken() || !this.oauthService.hasValidAccessToken()) {
         this.oauthService.initCodeFlow();
      }
    }).catch((err) => {
      console.log(err);
    });

但是,以下代码仍然返回false、false、null:

代码语言:javascript
复制
    const claims = this.oauthService.getIdentityClaims();
    console.log(this.oauthService.hasValidIdToken());
    console.log(this.oauthService.hasValidAccessToken());
    console.log(claims);

我的url中有一个code=,没有对服务的其他更改,似乎所有内容都已登录。我希望自己做了一些愚蠢的事情,或者误解了正在发生的事情,但任何建议都会有所帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-17 20:28:17

事实证明我做得不对。

我没有像我应该做的那样在我的构造函数中调用负载发现文档。

如果页面重新加载,我没有检查令牌的发现文档,因此它是无效的。

票数 0
EN

Stack Overflow用户

发布于 2021-05-20 00:54:15

我也有同样的问题,我解决了:

文件config.ts:

代码语言:javascript
复制
import { AuthConfig } from 'angular-oauth2-oidc';

export const authCodeFlowConfig: AuthConfig = {
    issuer: 'https://accounts.----/auth/realms/---',
    redirectUri: window.location.origin + '/login',
    clientId: 'my-project',
    responseType: 'code',
    scope: 'openid profile email offline_access',
    showDebugInformation: true,
};

文件login.ts:

代码语言:javascript
复制
ngOnInit(): void {
    this.oauthService.configure(authCodeFlowConfig);
    this.oauthService.tokenValidationHandler = new JwksValidationHandler();
    this.oauthService.setupAutomaticSilentRefresh();
    this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
      if (this.oauthService.hasValidAccessToken()) {
        // Load UserProfile to get the additional claims
        this.oauthService.loadUserProfile();
        this.router.navigateByUrl('/');
      } else {
        this.oauthService.initCodeFlow();
      }
    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61676890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档