我正在使用两个不同的Node.js项目,但我想将它们放在一起。
Azure应用程序都具有user_impersonation权限(用于租户/订阅信息)和user.read权限(配置文件信息)。
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
const main = async () => {
const creds: any = await msRestNodeAuth.interactiveLogin();
...continue - get tenant info from logged in user
}
我使用了此tutorial provided by Microsoft。我所做的唯一更改是添加user_impersonation的范围。
module.exports = {
appId: '1760c31a-....-baf68c2b3244',
redirectUri: 'http://localhost:3000',
scopes: [ 'user.read',
'user.impersonation'
]
};
它使用以下身份验证库和登录功能:
import { UserAgentApplication } from 'msal';
async login() {
try {
await this.userAgentApplication.loginPopup(
{
scopes: config.scopes,
prompt: "select_account"
});
await this.getUserProfile();
}
catch (err) {
var error = {};
if (typeof (err) === 'string') {
var errParts = err.split('|');
error = errParts.length > 1 ?
{ message: errParts[1], debug: errParts[0] } :
{ message: err };
} else {
error = {
message: err.message,
debug: JSON.stringify(err)
};
}
this.setState({
isAuthenticated: false,
user: {},
error: error
});
}
}
我收到错误:
{"errorCode":"invalid_scope","errorMessage":"The provided value for the input parameter 'scope' is not valid. The scope 'user.read user.impersonation openid profile' does not exist.","name":"ServerError"}
范围不正确。没有称为user.impersonation
的范围。应该是https://management.azure.com/user_impersonation
。
此外,您不能将一个令牌带给多个受众。 https://management.azure.com
用于天蓝色资源。 user.read
(https://graph.microsoft.com/User.Read
)用于Azure AD资源。
您应该针对不同的受众群体两次调用acquireTokenSilent
方法。
var accessToken = await this.userAgentApplication.acquireTokenSilent({
scopes: config.scopes
});
更新: