如何从Azure Active Directory获取对我的API和Microsoft Graph的有效访问令牌?

问题描述 投票:1回答:1

[我正在尝试建立一个spa javascript应用程序,该应用程序将用户登录到我们的Azure活动目录租户中,然后从Microsoft图形检索配置文件信息,并调用以C#核心(我的API)编写的Azure函数。

我为我的网站和Azure活动目录中的api分别设置了应用程序注册。

我正在使用javascript spa网站中的MSAL.js库,并且正在使用较新的Microsoft身份/ v2.0端点。

SPA应用程序将按预期的方式登录到活动目录,并且能够使用访问令牌来调用以图形化显示个人资料信息。在我的azure函数中,我验证令牌,并且失败,并显示错误“ IDX10511:签名验证失败。尝试过的键:.....”

现在,如果我在请求令牌时从范围中删除Microsoft图形,则会得到一个令牌,该令牌在传递给azure函数时可以很好地验证,但是我无法再在spa应用程序中检索配置文件数据?

我如何同时工作?

还值得注意的是,我用jwt.io测试了令牌,并且在使用图形时也无法验证签名。

[我在这里如何获取令牌:

var msalConfig = {
  auth: {
    redirectUri: window.location.origin, // forces top level instead of specific login pages - fixes deep link issues.
    clientId: "Client ID of the website app", //This is your client ID
    authority:
      "https://login.microsoftonline.com/my-tennant-guid" //This is your tenant info
  },

  cache: {
    cacheLocation: "localStorage",
    storeAuthStateInCookie: true
  }
};
const msalUserAgent = new Msal.UserAgentApplication(msalConfig);


var requestObj = {
  scopes: ["User.Read", "api://MyApi/Access"]
};

//when the spa starts up I login using redirects
msalUserAgent.loginRedirect(requestObj);


//then before calling an api I request a token using this method
 acquireTokenSilent() {
    var promise = msalUserAgent.acquireTokenSilent(requestObj);
    return promise;
  },


javascript azure .net-core azure-active-directory msal
1个回答
1
投票
请尝试在AcquisitionTokenSilent()函数中将范围指定为scopes: ["User.Read"]。由于访问令牌仅对一个API有效。如果需要两个,请使用不同的作用域两次调用acquireTokenSilent。

可以在登录时指定两个API的范围,但在获取令牌时不可以。令牌具有指定目标API的受众。因此,您不能针对一个API对另一个API使用令牌。这就是为什么它仅对一种API有效的原因。

© www.soinside.com 2019 - 2024. All rights reserved.