adal - 无法使用 adal.js 更新 token

标签 adal adal.js

我正在使用 Javascript(不带 Angular)向我的单页 Web 应用程序添加 AAD 身份验证。初始登录工作正常,但一个小时后, token 过期,我无法使用acquireToken 更新它。当我仍然使用我的客户端 ID 登录时,我尝试调用 AcquireToken 并且它工作正常,但是在 token 过期后,我无法更新它。它因“由于超时而导致 token 更新操作失败”而失败。

token 过期后,我运行了这个:

// ADALContext created from calling new AuthenticationContext
// passed in same clientID to acquire token as to create ADALContext 
ADALContext.acquireToken(clientID, function (error, token) {console.log(error, token)})

我在 AAD 中启用了 oauth2AllowImplicitFlow。
  "keyCredentials": [],
  "knownClientApplications": [],
  "logoutUrl": null,
  "oauth2AllowImplicitFlow": true,
  "oauth2AllowUrlPathMatching": true,

不知道我错过了什么步骤。谢谢!

编辑 : token 过期后,如果我运行acquireToken(clientID, func),我会得到“需要用户登录”。但是,如果我调用 getCachedUser,我会返回一个用户,然后调用acquireToken 返回超时错误。

最佳答案

我有同样的问题,我的修复工作。在 app.component.ts 中,将此代码添加到 ngOnit()。

this.adalService.handleWindowCallback();
this.adalService.acquireToken(this.adalService.config.loginResource).subscribe(token => {
  this.adalService.userInfo.token = token;
  if (this.adalService.userInfo.authenticated === false) {
    this.adalService.userInfo.authenticated = true;
    this.adalService.userInfo.error = '';
  }
}, error => {
  this.adalService.userInfo.authenticated = false;
  this.adalService.userInfo.error = error;
  this.adalService.login();
});

当 token 过期时,应用程序组件被调用,并获取 token 以静默方式刷新 token 。但是this.adalService.userInfo.authenticated仍然是 false 导致重定向或再次调用登录方法。因此,手动将其设置为 true 可修复重定向错误。 this.adalService.config.loginResource这是由 adal-angular 本身使用我们需要 token 的资源自动设置的。
同时添加 expireOffsetSeconds: 320 , 到 adal 配置数据设置以及
tenant: configData.adalConfig.tenant,
  clientId: configData.adalConfig.clientId,
  redirectUri: window.location.origin,
expireOffsetSeconds: 320

expireoffsetseconds 根据我们在其实际到期之前指定的时间使 token 无效。

关于adal - 无法使用 adal.js 更新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42235164/

相关文章:

azure - Web API 项目的 ADAL js 和 Azure AD 身份验证

Azure AD OpenID 连接 - token 中未显示组

azure - 隐藏 Azure Active Directory 登录页面中的后退按钮

angular - 找不到命名空间

ios - 如何根据 ADFS 3.0 识别通过 ADAL 身份验证的用户?

azure-active-directory - ADAL.js - 使用 id_token 获取 Microsoft Graph 访问 token

Azure应用程序注册: scope list not complete

c# - 使用 ADAL JS 的 Azure AD Graph API 不记名 token

javascript - 使用 ExtJS 应用程序更新 ADAL.js token