回答: 对于任何寻找此答案的人,我可以通过在 MSAL 获取令牌调用范围内指定 Web API 来获得所需的令牌,如下所示:
let token = msal.acquireToken({
scopes: [ 'myFunctionApp.azurewebsites.net/user_impersonation' ]
})
完成此操作后,我将令牌用作身份验证标头中的承载令牌。除了调用 MS Graph 端点外,我还可以使用它。我在这里一个安静的小地方找到了这些信息:
非常感谢@StanleyGong 的帮助,最终得到了答案。
/////
过去几天,我一直在寻找如何有效地保护从 Azure Web 应用调用到 Azure Function App 提供的 Vue SPA。我为 Web 应用程序和函数应用程序都打开了 Easy Auth,并且我正在调用/.auth/me
端点以获取一个 id 令牌,我读过它可以用作不记名令牌。所以我正在做的是调用/.auth/me
并使用返回的来创建一个 Authorization 标头,但是在调用函数应用程序时id_token
我仍然得到一个。401 Unauthorized
从 /.auth/me 获取 id_token 并将其作为默认标头添加到所有 Axios 调用的代码(我确实认识到这将需要刷新......我将在接到一个电话后创建所有这些逻辑在职的):
let authUrl = '/.auth/me';
let response = await axios.get(authUrl);
let token = response.data[0].id_token;
axios.defaults.headers.common['Authorization'] = "Bearer " + token;
我可以看到请求的 Authorization 标头中使用的令牌,紧跟上面的部分:
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Bearer eyJ0eXAiO...
我看到了这篇文章并尝试使用其中的一些信息,包括 Chris Gillum 的博客文章(很有帮助,但仍然没有让我到达那里):Azure Functions 的身份验证
关于如何使它工作的任何建议?我觉得我很接近,但我并不完全在那里。如果这不是正确的方法,那么任何建议也会有所帮助。
总的来说,我在身份方面非常缺乏经验,所以我一直在使用术语,这并没有因为文档的不一致而有所帮助。
另外,有没有什么方法可以测试这些东西,而无需在每次更改时都部署代码?通过以某种方式从商店获取令牌而不将代码放在网络应用程序服务器上来测试这一点会很棒......我猜是一厢情愿的想法,但只是想知道。
编辑:我刚刚意识到我读过的所有帖子都表明访问令牌是从返回的,/.auth/me
但我没有得到。返回的 JSON 如下所示,仅此而已:
id_token: "eyJ0eXA...7_A"
provider_name: "aad"
user_claims: [{typ: "aud", val: "2...fa"},…]
user_id: "<<my email address>>"
另一个编辑:我发现我可以使用 MSAL 获取令牌,但它包含的信息与/.auth/me
. 使用任一令牌作为不记名令牌仍会导致 401。我确实注意到在应用程序的 AAD 身份验证设置中,颁发者 URL 不同。一个是 sts.windows.net,另一个是 login.windows.net。两人后面都有租户 ID。不确定这是否有所作为,但我尝试将它们设置为相同的值,但没有帮助。
/.auth/me
令牌(当然是经过消毒的):
{
"aud": "2fe...fa", (AAD app id)
"iss": "https://sts.windows.net/<< AD tenant id >>/",
"iat": 15785xxx,
"nbf": 15785xxx,
"exp": 15785xxx,
"aio": "AVQAq/...UQ=",
"amr": [
"pwd",
"mfa"
],
"family_name": "<< my last name >>",
"given_name": "<< my first name >>",
"ipaddr": "<< my ip >>",
"name": "<< my full name >>",
"nonce": "e32a...48",
"oid": "a0...0e",
"sub": "LNu...8l8",
"tid": "f14...2ca",
"unique_name": "<< my email >>",
"upn": "<< my email >>",
"uti": "i9O...TAQ",
"ver": "1.0"
}
MSAL 访问令牌:
{
"aud": "000...000", (mostly 0s...not sure what this id is)
"iss": "https://sts.windows.net/<< AD tenant id >>",
"iat": 15785xxx,
"nbf": 15785xxx,
"exp": 15785xxx,
"acct": 0,
"acr": "1",
"aio": "AVQAq/8O...ZZ12s=", (different than above)
"amr": [
"pwd",
"mfa"
],
"app_displayname": "<< app name in AAD app registration >>",
"appid": "<< app GUID from AAD >>",
"appidacr": "0",
"family_name": "<< my last name >>",
"given_name": "<< my first name >>",
"ipaddr": "<< my ip >>",
"name": "<< my full name >>",
"oid": "a0...0e", (same as above)
"platf": "3",
"puid": "10...1B",
"scp": "User.Read profile openid email",
"signin_state": [
"kmsi"
],
"sub": "WuX...L3A",
"tid": "f14...2ca", (tenant id, same as above)
"unique_name": "<< my email >>",
"upn": "<< my email >>",
"uti": "UD...AA",
"ver": "1.0",
"xms_st": {
"sub": "LNu...8l8"
},
"xms_tcdt": 14...37
}