当前位置: 首页 > 知识库问答 >
问题:

passport-azure-ad:前端和后端具有相同的标记(什么策略,如何实现?)

阎丰羽
2023-03-14
    null

我想用“Passport-Azure-AD”,但我不知道该怎么做?OIDCStrategy还是BearerStrategy?

我可以轻松地使用OIDCStrategy进行前端登录,但我没有“承载者”,所以我不明白如何使用相同的“登录”来调用后端API?使用BearerStrategy,我尝试通过获取'https://login.microsoftonline.com/'+myTenantid+'/oauth2/v2.0/authorize?response_type=code&client_id='+myClientId+'&redirect_uri=HTTP%3a%2f%2flocalhost%3a3000%2fCallback&scope=openid'来获取承载,但每次当我在jwt.io上测试它时,它都作为无效承载返回(“无效签名”),并且我不能在后端服务器中使用该承载。

我有点困惑怎么办?我需要帮助。

谢谢

共有1个答案

郭琦
2023-03-14

如本文所示,您可以进行HTTP调用以获取访问令牌。你可以和邮差一起试试。

在node.js中使用代表流:

var qs = require("querystring");

var http = require("https");

var options = { "method": "POST", "hostname": [ "login", "microsoftonline", "com" ], "path": [ "b2bc09c8-9386-47xxxxxxxx", "oauth2", "token" ], "headers": { "Content-Type": "application/x-www-form-urlencoded", "cache-control": "no-cache", "Postman-Token": "739540c9-1e3d-4d74-bxxxxxxx" } };

var req = http.request(options, function (res) { var chunks = [];

res.on("data", function (chunk) {
    chunks.push(chunk);
});

res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
});
});

req.write(qs.stringify({ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', client_id: '<your-application-id>', resource: 'https://graph.microsoft.com/', client_secret: '<your-client-secret>', scope: 'openid', assertion: 'xxxxx', requested_token_use: 'on_behalf_of', undefined: undefined })); 
req.end();

您可以提取访问令牌并将其用于承载请求中的资源。

 类似资料: