首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >API不会使用MSAL通过从SPA传递的令牌对AAD进行身份验证

API不会使用MSAL通过从SPA传递的令牌对AAD进行身份验证
EN

Stack Overflow用户
提问于 2017-07-25 01:14:29
回答 2查看 1.4K关注 0票数 2

我正在尝试调整引用为herehere的示例B2C代码,以针对我们的组织AAD工作。

我有一个SPA应用程序,它通过MSAL成功地与AAD进行身份验证,并接收令牌。SPA应用程序可以使用令牌从MS Graph API中提取数据--因此我确信令牌是有效的。

SPA当前正在本地运行@ localhost:9000。

我还有一个运行@ localhost:3000的Nodejs API应用程序。SPA应用程序调用应用程序接口,传递用于GraphAPI的相同令牌。API应用程序应该使用该令牌对用户进行身份验证,并提供对API的访问;然而,我只得到了一个401 - Unauthorized。

(我正在使用Aurelia作为我的客户端代码。使用Aurelia的HTTP客户端发出HTTP请求)。

以下是用于调用API的SPA代码:

代码语言:javascript
代码运行次数:0
运行
复制
//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) Nodejs代码:

代码语言:javascript
代码运行次数:0
运行
复制
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)
})

我相信我只是误解了这一切是如何工作的,但我希望能得到一些指导,关于我需要做些什么来让它工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-25 01:46:43

令牌类似于银行支票:它们只能由为其编写的人进行缓存。为MS Graph颁发的令牌将以Microsoft Graph作为受众,并且仅应由Microsoft Graph接受。任何其他接收该令牌的API X都应该拒绝该令牌。打算调用X的客户端应该为X请求一个令牌,而不管它是否已经拥有另一个服务的令牌(如Microsoft图形)。这里有一个使用ADAL JS:https://azure.microsoft.com/en-us/resources/samples/active-directory-angularjs-singlepageapp-dotnet-webapi/实现的流程示例:在这一点上,Azure AD API(由MSAL使用)不能发布自定义v2的访问令牌:该功能正在开发中,但我们不知道何时它将在生产中可用。在此之前,v2不支持这种拓扑(因此使用MSAL )。抱歉的!

票数 2
EN

Stack Overflow用户

发布于 2018-07-13 21:02:28

一年后,现在可以使用MSAL保护您的自定义API。在应用程序门户(https://apps.dev.microsoft.com)中,您可以创建您的应用程序,然后“添加平台”两次-一次用于您的web应用程序SPA,另一次用于您的web api。在web api中,创建一个“作用域”,例如"api://e892d79e-03e7-4e5e-.../access_as_user",,然后将该作用域传递给acquireTokenSilent调用(而不是上面示例中的"user.read“)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45286400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档