rob*_*r78 1 azure access-token single-page-application azure-active-directory azure-ad-msal
(问题的继续:为什么我的 SPA 正在使用 Azure Active Directory 调用我的 WebAPI,收到“此请求的授权已被拒绝。”?)
我的客户端 SPA 正在尝试调用受保护的 WebAPI(服务)。客户端使用 MSAL(Micosoft 身份验证库)。问题发生在调用 API 之前,即在下图中的 1 和 2 之间。
这是客户
<!DOCTYPE html>
<html>
<head>
<title>Quickstart for MSAL JS</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js"></script>
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.2/js/msal.js"></script>
</head>
<body>
<div class="container">
<div>
<button id="GetTodos" onclick="getTodos()">Get Todos</button>
</div>
</div>
<script>
var msalConfig = {
auth: {
clientId: 'xxxxxxxxxxxxxxxxxxxxxxx2427',
authority: "https://login.microsoftonline.com/my.tenant"
},
cache: {
cacheLocation: "sessionStorage",
storeAuthStateInCookie: true
}
};
var requestObj = {
// scopes: ["user.read"]
scopes: ["access_as_user"]
};
var myMSALObj = new Msal.UserAgentApplication(msalConfig);
myMSALObj.handleRedirectCallback(authRedirectCallBack);
function getTodos() {
console.log("getTodos ...");
myMSALObj.loginPopup(requestObj)
.then(response => {
myMSALObj.acquireTokenPopup(requestObj).then(function (tokenResponse) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log("success");
//this.responseText
} else {
console.log("failure");
}
}
const apiUrl = "https://localhost:44321/api/todolist";
xmlHttp.open("GET", apiUrl, true); // true for asynchronous
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + tokenResponse.accessToken);
xmlHttp.send();
}).catch(function (error) {
console.log(error);
});
})
.catch(err => {
// handle error
console.log("login popup failed.", err);
});
}
function authRedirectCallBack(error, response) {
console.log('authRedirectCallBack');
if (error) {
console.log(error);
} else {
if (response.tokenType === "access_token") {
console.log("response.tokenType === access_token");
} else {
console.log("token type is:" + response.tokenType);
}
}
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
客户端和服务都是 Azure Active Directory 上的注册应用程序
客户端具有访问服务的 API 权限。
该服务确实公开了一个范围为 access_as_user 的 API
然而调用
myMSALObj.loginPopup(requestObj)
Run Code Online (Sandbox Code Playgroud)
原因
ServerError: "AADSTS650053: The application 'ClientSPA' asked for scope 'access_as_user' that doesn't exist on the resource '00000003-0000-0000-c000-000000000000'.
Run Code Online (Sandbox Code Playgroud)
使用 Chrome 时,我收到此消息
进一步的调查
我没有要求范围“access_as_user”,而是要求“user.read”这给了我一个访问令牌。但这会导致 WebAPI 响应
Authorization has been denied for this request
Run Code Online (Sandbox Code Playgroud)
当我解码访问令牌时,我看到观众是https://graph.microsoft.com。但我希望观众是“ https://my.tenant/TodoListService-NativeDotNet ”。请参见下图(混淆的行确实包含特定于我的用户和我注册的应用程序的信息)
问题
资源 '00000003-0000-0000-c000-000000000000' ID 来自哪里(错误消息中提到)?它不是客户端的应用程序 ID,也不是服务的应用程序 ID。
我在 Azure Active Directory 或客户端的配置中做错了什么?
CORS 会不会有问题?我已设置服务以允许 CORS。
范围应包括公开资源的标识符(应用程序 ID URI)。您可以通过转到“公开 API” -> 编辑范围 -> 复制顶部的标签来找到完整的值...
var requestObj = {
// scopes: ["https://graph.microsoft.com/user.read"]
scopes: ["https://ServiceWebAPI/TodoListService-NativeDotNet-access_as_user"]
};
Run Code Online (Sandbox Code Playgroud)
在此处进一步阅读各个范围:https : //docs.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent#requesting-individual-user-consent
| 归档时间: |
|
| 查看次数: |
2368 次 |
| 最近记录: |