因此,我已经成功地在我的angular站点中集成了Azure AD身份验证,按照msal-angular中的说明,现在我正在寻求定义和利用角色和权限,以提供对用户可以和不能做什么的更细粒度的控制。
根据我所能确定的,我可以通过遵循这组指令(https://docs.microsoft.com/en-us/azure/architecture/multitenant-identity/app-roles)来定义角色,但是msal-angular似乎不会在登录时公开这一点--或者至少我还没有找到关于如何做到这一点的指令。
也许我错过了什么。如有任何建议,我们将不胜感激。
发布于 2019-05-09 05:07:01
要获取用户所属的组,你需要将directory.read.all添加到你的Angular应用程序中的作用域以及Azure应用程序设置的API权限中。
let graphUser = await graphClient.api('/me').get();
let graphUserGroups = await graphClient.api(`/users/${graphUser.id}/memberOf`).get();
let user = new User();
user.id = graphUser.id;
user.displayName = graphUser.displayName;
// Prefer the mail property, but fall back to userPrincipalName
user.email = graphUser.mail || graphUser.userPrincipalName;
graphUserGroups.value.forEach(group => {
user.groups.push({
group_id: group.id,
group_name: group.displayName
});
});发布于 2019-05-08 20:55:21
(感谢stillatmylinux)
仅供参考:以下是我的angular 7解决方案(为了可读性起见进行了简化):
import { MsalService, BroadcastService } from '@azure/msal-angular';
import { Client } from '@microsoft/microsoft-graph-client';
private subscription: Subscription;
private graphClient: Client;
private memberRoles: any[];
constructor(
readonly auth: MsalService,
readonly broadcast: BroadcastService
) {
// Initialize Microsoft Graph client
this.graphClient = Client.init({
authProvider: async (done) => {
let token = await this.auth.acquireTokenSilent(["User.Read", "Directory.Read.All"])
.catch((reason) => {
done(reason, null);
});
if (token) {
done(null, token);
} else {
done("Could not get an access token", null);
}
}
});
this.subscription = broadcast.subscribe("msal:loginSuccess",
() => {
//Get associated member roles
this.graphClient.api('/me/memberOf').get()
.then((response) => {
this.memberRoles = response.value;
}, (error) => {
console.log('getMemberRoles() - error');
});
});
}发布于 2019-05-09 17:01:10
您可以更改清单以在令牌本身中传递这些内容。
sample (虽然是针对.NET的)详细解释了这一点
https://stackoverflow.com/questions/56041125
复制相似问题