【发布时间】:2021-01-22 05:25:43
【问题描述】:
我有一个 angular 应用程序和一个 DotNet 核心 web api 应用程序。 Web api 公开了 2 个权限。角度已订阅这些权限(见屏幕截图)。
在代码中,我试过这个
export const loginRequest: { scopes: string[] } = {
//scopes: ['user.read', 'openid', 'profile'], //--> I commented out this line.
scopes: []
};
export const tokenRequest: { scopes: string[] } = {
scopes: ['api://da8b9450-d9b7-4b7f-9667-fdae9a7c8359/API.Access',
'api://da8b9450-d9b7-4b7f-9667-fdae9a7c8359/API.Write']
};
consentScopes: [
...loginRequest.scopes,
...tokenRequest.scopes,
],
访问令牌包含我没有要求的范围,但它没有返回我要求的范围。
如何获得我请求的 2 个作用域?
感谢您的帮助
编辑 1
这是配置
auth: {
clientId: '78803184-e866-4966-b372-d98b4feae898',
authority: "https://login.microsoftonline.com/{tenantId}/",
validateAuthority: true,
redirectUri: "http://localhost:4200/",
postLogoutRedirectUri: "http://localhost:4200/",
navigateToLoginRequestUrl: true,
}
编辑 2
这是现在请求的范围。我已经删除了所有相关的图表,例如user.read、openid 和profile。
{
popUp: !isIE,
consentScopes: [
"api://da8b9450-d9b7-4b7f-9667-fdae9a7c8359/API.Access",
"api://da8b9450-d9b7-4b7f-9667-fdae9a7c8359/API.Write"
],
unprotectedResources: ["https://localhost:5001"],
protectedResourceMap,
extraQueryParameters: {}
}
我仍然收到相同的范围,即即使在客户端到 API 的客户端列表之后。
但是,我查看了发送到 AZURE AD 的请求。这就是它的样子。根据此请求,我仍在请求 user.read、openid 和 profile,尽管我已将它们从请求范围列表中删除。
Request URL: https://login.microsoftonline.com/313200b5-a917-47d1-2233-149b07d5d7b5/oauth2/v2.0/authorize?
response_type=token&scope=user.read openid profile&client_id=78803184-e866-54e3-b200-d98b4feae898
&redirect_uri=http://localhost:4200/
&state=eyJpZCI6IjMwZDJkOGNkLTM2NWUtNGMwOS1iYWY1LTcyZWYyMTU0YWE5ZSIs
InRzIjoxNjExNTUwMDUxLCJtZXRob2QiOiJzaWxlbnRJbnRlcmFjdGlvbiJ9
&nonce=6f25b4e0-71f7-4cde-abf4-cb5545d2507e
&client_info=1&x-client-SKU=MSAL.JS&x-client-Ver=1.4.4
&login_hint=admin@myemail.com
&client-request-id=a68ef0b3-111b-4b1c-a3e7-cdcb075516ca
&prompt=none&response_mode=fragment
编辑 3
我找到了这行代码
const GRAPH_ENDPOINT = 'https://graph.microsoft.com/v1.0/me';
getProfile() {
this.http.get(GRAPH_ENDPOINT)
.toPromise().then(profile => {
this.profile = profile;
});
}
【问题讨论】:
-
这是正常的。你得到的是 ms graph api 令牌,它只包含 ms graph api 的范围。一个令牌不能包含两个 API 的范围。如果需要获取自定义 api 的范围,则应该获取另一个令牌。
标签: angular azure-active-directory msal.js