401错误-调用受AZURE Active Directory保护的REST API(JavaScript客户端)
大家好, 我得到了一个场景,在这个场景中,我应该调用一个受AZURE ACTIVE DIRECTORY保护的REST api。大多数代码运行良好,我也可以使用myMSALObj.acquireTokenSilent函数获取令牌 401错误发生在我使用该令牌发送AJAX调用时,您可以看到以下代码 用户在Active Directory中,我得到了相应的令牌,我不知道为什么我的ajax调用在我将令牌发送到RESTAPI时失败,请帮助401错误-调用受AZURE Active Directory保护的REST API(JavaScript客户端),javascript,azure-active-directory,http-status-code-401,restapi,Javascript,Azure Active Directory,Http Status Code 401,Restapi,大家好, 我得到了一个场景,在这个场景中,我应该调用一个受AZURE ACTIVE DIRECTORY保护的REST api。大多数代码运行良好,我也可以使用myMSALObj.acquireTokenSilent函数获取令牌 401错误发生在我使用该令牌发送AJAX调用时,您可以看到以下代码 用户在Active Directory中,我得到了相应的令牌,我不知道为什么我的ajax调用在我将令牌发送到RESTAPI时失败,请帮助 <script type="text/j
<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>
常量msalConfig={
认证:{
客户ID:“94edf294-08ae-489f-8621-c6d98009afa8”,
权限:“https://login.microsoftonline.com/c483b3c4-21a6-4c93-95ea-7436cf318a68",
重定向URI:“https://localhost:44334/",
},
缓存:{
cacheLocation:“sessionStorage”//此选项配置缓存的存储位置
storeAuthStateInCookie:false,//如果您在IE11或Edge上遇到问题,请将其设置为“true”
}
};
const myMSALObj=新的Msal.UserAgentApplication(msalConfig);
函数CallApi(){
//添加要在Microsoft identity platform终结点上使用的id令牌的作用域。
常量登录请求={
作用域:[“User.Read”],
};
myMSALObj.loginPopup(loginRequest)
.然后((登录回复)=>{
常量accessTokenRequest={
范围:[”api://8c2b0253-f9e8-442c-bccf-b4a8bbe73b59/access_as_user"]
};
myMSALObj.acquireTokenSilent(accessTokenRequest)。然后((accessTokenResponse)=>{
var accessToken=accessTokenResponse.accessToken;
var apidentpoint=”https://localhost:44387/api/hello";
var bearer=“bearer”+accessToken;
log(“accessToken=”,accessToken);
$.ajax({
url:Apident,
键入:“获取”,
beforeSend:function(xhr){xhr.setRequestHeader(“Authorization”,bearer)}
}).完成(功能(响应){
警惕(“成功”);
log(“response=,JSON.stringify(response));
}).失败(功能(错误){
控制台。错误(“发生错误”);
log(“error=,JSON.stringify(err));
});
})
}).catch(函数(错误){
console.log(错误);
});
}
新API代码的屏幕截图
JWT.ms(访问令牌)截图
JWT代币的新屏幕截图
您不应将
appsettings.json文件中的客户端ID
设置为api应用程序的客户端ID
。它应该是您的客户端应用程序的客户端id
。根据您提供的jwt分析图,我认为应该是:94edf294-08ae-489f-8621-c6xxxxxxx
我的启动.cs
文件中缺少我的坏的后续调用
app.UseAuthentication();
谢谢你们的帮助
特别是-@Carl Zhao,@Purushothaman@Rass Masood您是否已将您的API公开给客户端应用程序注册?用于解析您的令牌并提供屏幕截图。您必须确保客户端应用程序请求的范围与令牌的预期收件人一致。检查您的
aud
以确保它是您要调用的api,以及401的原因您可能使用了错误的令牌,或者您使用了不属于api的令牌来调用api。@Purushothaman我看不出您的代码有任何问题。我认为这个问题仍然发生在你的生活中。您的api端点是自定义api吗?确保它是您的令牌收件人。您可以在本文档的web api中找到实现身份验证和授权的详细信息。我今天会试试的。我刚试过,也有同样的错误。。。我刚刚为这个问题附上了一个新的截图。请查收that@Imran您可能误解了我的意思,我的意思是您应该将appsettings.json文件中的客户端id更改为:94edf294-08ae-489f-8621-c6xxxxxx。是的,我在appsettings.json文件中更改了ClientId,然后附上显示新ClientId的JWT令牌的新屏幕截图。