401 错误 - 使用 AZURE Active Directory(JavaScript 客户端)保护调用 REST API
401 Error - Call REST API secured with AZURE Active Directory (JavaScript Client)
大家好,
我有一个场景,我应该调用一个由 AZURE ACTIVE DIRECTORY 保护的 REST api。大多数代码运行良好,我也可以使用 myMSALObj.acquireTokenSilent 函数获取令牌。
401 当我使用该令牌发送 AJAX 调用时出现错误,您可以看到以下代码
用户在 Active Directory 中,我为其获得了正确的令牌,我不知道为什么当我将该令牌发送到 rest-api 时我的 ajax 调用失败,请帮助
<script type="text/javascript">
const msalConfig = {
auth: {
clientId: "94edf294-08ae-489f-8621-c6d98009afa8",
authority: "https://login.microsoftonline.com/c483b3c4-21a6-4c93-95ea-7436cf318a68",
redirectUri: "https://localhost:44334/",
},
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
}
};
const myMSALObj = new Msal.UserAgentApplication(msalConfig);
function CallApi() {
// Add scopes for the id token to be used at Microsoft identity platform endpoints.
const loginRequest = {
scopes: ["User.Read"],
};
myMSALObj.loginPopup(loginRequest)
.then((loginResponse) => {
const accessTokenRequest = {
scopes: ["api://8c2b0253-f9e8-442c-bccf-b4a8bbe73b59/access_as_user"]
};
myMSALObj.acquireTokenSilent(accessTokenRequest).then((accessTokenResponse) => {
var accessToken = accessTokenResponse.accessToken;
var apiEndpoint = "https://localhost:44387/api/hello";
var bearer = "Bearer " + accessToken;
console.log("accessToken = ", accessToken);
$.ajax({
url: apiEndpoint,
type: "GET",
beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", bearer) }
}).done(function (response) {
alert("SUCCESS");
console.log("response = ", JSON.stringify(response));
}).fail(function (err) {
console.error("Error Occurred");
console.log("error = ", JSON.stringify(err));
});
})
}).catch(function (error) {
console.log(error);
});
}
</script>
新 API 代码的屏幕截图
JWT.ms(访问令牌)的屏幕截图
JWT 令牌新截图
您不应将 appsettings.json file
中的 client ID
设置为 api app
的 client id
。它应该是您 client app
的客户端 ID。根据你提供的jwt分析图,我觉得应该是:94edf294- 08ae-489f-8621-c6xxxxxxx
.
糟糕,我的 startup.cs 文件中缺少后续调用
app.UseAuthentication();
感谢大家的帮助
特别 - @Carl Zhao,@Purushothaman @Rass Masood
大家好,
我有一个场景,我应该调用一个由 AZURE ACTIVE DIRECTORY 保护的 REST api。大多数代码运行良好,我也可以使用 myMSALObj.acquireTokenSilent 函数获取令牌。
401 当我使用该令牌发送 AJAX 调用时出现错误,您可以看到以下代码
用户在 Active Directory 中,我为其获得了正确的令牌,我不知道为什么当我将该令牌发送到 rest-api 时我的 ajax 调用失败,请帮助
<script type="text/javascript">
const msalConfig = {
auth: {
clientId: "94edf294-08ae-489f-8621-c6d98009afa8",
authority: "https://login.microsoftonline.com/c483b3c4-21a6-4c93-95ea-7436cf318a68",
redirectUri: "https://localhost:44334/",
},
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
}
};
const myMSALObj = new Msal.UserAgentApplication(msalConfig);
function CallApi() {
// Add scopes for the id token to be used at Microsoft identity platform endpoints.
const loginRequest = {
scopes: ["User.Read"],
};
myMSALObj.loginPopup(loginRequest)
.then((loginResponse) => {
const accessTokenRequest = {
scopes: ["api://8c2b0253-f9e8-442c-bccf-b4a8bbe73b59/access_as_user"]
};
myMSALObj.acquireTokenSilent(accessTokenRequest).then((accessTokenResponse) => {
var accessToken = accessTokenResponse.accessToken;
var apiEndpoint = "https://localhost:44387/api/hello";
var bearer = "Bearer " + accessToken;
console.log("accessToken = ", accessToken);
$.ajax({
url: apiEndpoint,
type: "GET",
beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", bearer) }
}).done(function (response) {
alert("SUCCESS");
console.log("response = ", JSON.stringify(response));
}).fail(function (err) {
console.error("Error Occurred");
console.log("error = ", JSON.stringify(err));
});
})
}).catch(function (error) {
console.log(error);
});
}
</script>
新 API 代码的屏幕截图
JWT.ms(访问令牌)的屏幕截图
JWT 令牌新截图
您不应将 appsettings.json file
中的 client ID
设置为 api app
的 client id
。它应该是您 client app
的客户端 ID。根据你提供的jwt分析图,我觉得应该是:94edf294- 08ae-489f-8621-c6xxxxxxx
.
糟糕,我的 startup.cs 文件中缺少后续调用
app.UseAuthentication();
感谢大家的帮助
特别 - @Carl Zhao,@Purushothaman @Rass Masood