node.js - @azure/msal Node : Is there a way to log out/invalidate tokens?

标签 node.js authentication azure-active-directory msal

我正在使用 @azure/msal-node打包在 Node 应用程序中,以使我的用户能够使用他们的 AzureAD 凭据登录。登录和获取 session token 工作正常,但我找不到使 session 无效/注销用户的方法 - 我在这里忽略了一些明显的东西吗?
就上下文而言,这是我获取 token 的方式:

// msalConfig is my valid config object
const msalApp = new msal.ConfidentialClientApplication(msalConfig); 

const authCodeUrlParameters = {
  scopes: ['user.read'],
  redirectUri: BASE_URL + '/msal-redirect'
};

try {
  const authCodeResponse = await msalApp.getAuthCodeUrl(authCodeUrlParameters);
  reply.redirect(authCodeResponse);
} catch (e) {
  logError('auth code redirect error', e);
}
在重定向处理程序中,我这样做:
const tokenResponse = await msalApp.acquireTokenByCode({
  code: request.query.code,
  scopes: ['user.read'],
  redirectUri: BASE_URL + '/msal-redirect'
});
然后我使用该 token 来显示登录用户等。
我缺少的是 msalApp.logout() - 我在这里没看到什么?

最佳答案

不幸的是,MSAL 目前不包含 msalApp.logout() API。相反,您将不得不手动实现这些步骤。
注销操作将包含多个步骤:

  • 从 msal 应用程序缓存中删除帐户和 token 。
  • 重定向到 AAD 注销端点,以便用户注销并删除 AAD cookie。
  • 如果您的 webapp 有 session ,则使其无效。

  • 要从 msal 应用程序缓存中删除帐户和 token ,您可以执行以下操作:
    const accounts = msalApp.getTokenCache().getAllAccounts();
    // filter on the account that you want to delete from the cache.
    // I take the first one here to keep the code sample short
    const account = accounts[0];
    msalApp.getTokenCache().removeAccount(account);
    
    要从 AAD 注销,您必须将用户重定向到 Azure AD 注销终结点。 documentation here应该解释如何制作这个请求。

    关于node.js - @azure/msal Node : Is there a way to log out/invalidate tokens?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64199637/

    相关文章:

    node.js - Node js MySQL 连接问题

    asp.net - .ASPXAUTH cookie 在 session 结束时过期

    azure - 在 Azure AD 中,如何通过 PowerShell 设置和读取用户的主电子邮件(例如 Set-AzureADUser 和 Get-AzureADUser)?

    php - Azure - 使用 Microsoft Office 365 帐户登录

    node.js - NodeJS 动画 gif 调整大小

    当 Node js 调用时,Python3 不会写入文件

    node.js - 如何在 AWS Cognito 中创建身份提供商时启用 idp 注销流程?

    http - 静态资源的基本身份验证

    authentication - 如何使用 Nuxt.js 将数据存储到本地存储

    c# - 如何将 Azure AD 身份验证与 Redis 缓存结合使用