我正在使用“adal-node”npm 包通过 AzureAD 进行身份验证。一切正常,我得到了一个令牌。
但是,在检查 JWT 令牌中的“aud”声明时,我看到受众 GUID 的前缀为“spn:”。我认为当我尝试在现有的 Web API 上使用 JWT 令牌时,这会给我带来问题。当我使用相同的 AzureAD 通过 Web 应用程序进行身份验证时,“aud”声明不以“spn:”为前缀,并且我能够在 WebAPI 上调用端点。
任何人都可以阐明这一点吗?这是经过一番努力才得以实现的最后一个障碍。
Update:
在收到 JWT 令牌后,我立即使用 npm 包“azure-ad-jwt”通过 AzureAD 验证 JWT 令牌,但出现了我怀疑是问题所在的错误 -“JWT 受众无效”。它期望“aud”声明没有“spn:”前缀。这个 spn 前缀来自哪里?
这是我的 app.js
var adal = require('adal-node');
var activeDirectoryEndpointUrl = 'https://login.microsoftonline.com/';
var options = {
domain: '<AzureAD GUID>',
activeDirectoryResourceId: '<AzureAD App Client ID 1>',
clientId: '<AzureAD App Client ID 2>'
};
var tokenCache = new adal.MemoryCache();
var authorityUrl = activeDirectoryEndpointUrl + options.domain;
var context = new adal.AuthenticationContext(authorityUrl, true, tokenCache);
context.acquireUserCode(options.activeDirectoryResourceId, options.clientId, 'en-us', function (err, userCodeResponse) {
if (err) {
console.error(err);
return;
}
console.log('Use a web browser to open the page ' + userCodeResponse.verificationUrl + ' and enter the code ' + userCodeResponse.userCode + ' to sign in.');
context.acquireTokenWithDeviceCode(options.activeDirectoryResourceId, options.clientId, userCodeResponse, function (err, tokenResponse) {
if (err) {
console.error(err);
return;
}
console.log(tokenResponse);
});
});
解码后的 JWT 令牌:
{
"typ":"JWT",
"alg":"RS256",
"x5t":"XXXXXXX",
"kid":"XXXXXXX"
}
{
"aud":"spn:XXXXXXX", // <<< Offending claim
"iss":"https://sts.windows.net/XXXXXXX/",
"iat":1471355868,
"nbf":1471355868,
"exp":1471359768,
"acr":"1",
"amr":["pwd"],
"appid":"XXXXXXX",
"appidacr":"0",
"e_exp":7200,
"family_name":"XX",
"given_name":"XX",
"ipaddr":"XX.XX.XX.XX",
"name":"XX XX",
"oid":"XXXXXXX",
"scp":"user_impersonation",
"sub":"XXXXXXX",
"tid":"XXXXXXX",
"unique_name":"[email protected] /cdn-cgi/l/email-protection",
"upn":"[email protected] /cdn-cgi/l/email-protection",
"ver":"1.0"
}