我正在尝试调整引用的示例 B2C 代码 here和 here与我们的组织 AAD 作对。
我有一个 SPA 应用程序,它已通过 MSAL 成功通过 AAD 进行身份验证并接收 token 。 SPA 应用程序可以使用 token 从 MS Graph API 提取数据 - 因此我确信 token 是有效的。
SPA 当前正在本地运行 @ localhost:9000。
我有第二个应用程序,它是运行 @ localhost:3000 的 Nodejs API。 SPA 应用程序调用 API,并传递用于 GraphAPI 的相同 token 。 API 应用程序应该使用该 token 来验证用户身份并提供对 API 的访问;但是,我只收到 401 - 未经授权。
(我使用 Aurelia 作为客户端代码。HTTP 请求是使用 Aurelia 的 HTTP 客户端发出的)。
以下是用于调用 API 的 SPA 代码:
//Not entirely sure what to use as the scope here!! API is registered to allow user.read Graph API scope.
this.config.msClient.acquireTokenSilent(["user.read"]).then(token => {
console.log("Token acquired silently.");
this.makeAPICall(token);
}, error => { ... }
);
makeAPICall(token) {
this.http.configure(config => {
config
.withBaseUrl('http://localhost:3000/')
.withDefaults({
headers: {
'Authorization': 'Bearer ' + token
}
}
this.http.fetch("user/0")
.then(response => {
debugger; //response is 401 Unauthenticated at this point
}, error => { ... });
}
我已在 apps.dev.microsoft.com 上为我的 API 注册了一个应用程序。这是我的服务器端(API)Nodejs 代码:
const express = require("express")
const port = 3000
const passport = require("passport")
var BearerStrategy = require("passport-azure-ad").BearerStrategy;
var userList = [
{id: 1, first: "Mickey", last: "Mouse", age: 95},
{id: 2, first: "Donald", last: "Duck", age: 85},
{id: 3, first: "Pluto", last: "leDog", age: 70}
]
var options = {
identityMetadata: "https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration",
clientID: "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx", //My registered App Id
passReqToCallback: false,
validateIssuer: true,
issuer: "xxxxxxx" //Our tenant name
};
var strategy = new BearerStrategy(options, (token, done) => {
console.log(token);
done(null,{}, {scope: '*'});
})
const app = express()
app.use(passport.initialize())
passport.use(strategy);
//Allow CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Headers","Authorization, Origin, X-Requested-With, Content-Type,Accept");
next();
})
app.get("/",(request, response) => { //Unauthenticated -- always works
response.send("Hello World!")
})
app.get("/user/:id",passport.authenticate('oauth-bearer',{session: false }),
(request, response) => {
//Never seem to get here (when debugging) as authenticate always fails
let id = request.params["id"];
let user = userList[id];
if(user) response.json(user);
else response.send("No user found")
})
app.listen(port, () => {
console.log("Listening on port " + port)
})
我确信我只是误解了这一切是如何工作的,但希望得到一些关于我需要更改哪些内容才能使其正常工作的指导。
最佳答案
token 就像银行支票:它们只能由为其编写的人缓存。为 MS Graph 发行的 token 将作为 Microsoft Graph 的受众,并且只能被 Microsoft Graph 接受。接收该 token 的任何其他 API X 都应拒绝它。打算调用 X 的客户端应该请求 X 的 token ,无论它是否已经拥有其他服务(如 Microsoft 图)的 token 。下面是使用 ADAL JS 实现的该流程的示例:https://azure.microsoft.com/en-us/resources/samples/active-directory-angularjs-singlepageapp-dotnet-webapi/ 目前,Azure AD v2(由 MSAL 使用)无法为自定义 API 颁发访问 token :该功能正在开发中,但我们尚不清楚该功能何时在生产中可用。在此之前,v2 不支持此拓扑(因此是 MSAL JS)。抱歉!
关于node.js - API 不会使用 MSAL 从 SPA 传递的 token 针对 AAD 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45286400/