使用node.js检索来自azure ad b2c登录的声明

时间:2019-02-06 19:20:01

标签: node.js azure azure-ad-b2c

我正在使用Azure AD B2C执行身份验证。成功登录后,我在浏览器中看到如下内容:

localhost:3000 /#id_token = eyJ0elll ...

我想知道如何在我的Nodejs后端中检索该令牌

1 个答案:

答案 0 :(得分:0)

听起来您像按照教程Azure Active Directory B2C: Web sign-in with OpenID Connect一样发送了https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/oauth2/v2.0/authorize?.....这样的url身份验证请求并获得了30x响应以重定向到浏览器中的位置localhost:3000/#id_token=eyJ0elll...&code=AwABAAAA...&state=arb....。现在,您想在没有浏览器的NodeJS中实现它。

这是我使用request/request的简单代码。

const request = require('request');
var url = require("url");
var querystring = require("querystring");
var authorize_uri = 'https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/oauth2/v2.0/authorize?.....';
request(authorize_uri, function (error, response, body) {
    var href = response.request.href; // get localhost:3000/#id_token=eyJ0elll...&code=AwABAAAA...&state=arb....
    var hash = url.parse(href).hash; // get #id_token=eyJ0elll...&code=AwABAAAA...&state=arb....
    //var hash = response.request.uri.hash; // or directly get #id_token=eyJ0elll...&code=AwABAAAA...&state=arb....
    var id_token = querystring.parse(hash)['#id_token'];
    console.log(id_token);
});
相关问题