首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用MSAL-ANGULAR读取角色/权限

如何使用MSAL-ANGULAR读取角色/权限
EN

Stack Overflow用户
提问于 2019-05-08 20:44:50
回答 6查看 7.9K关注 0票数 6

因此,我已经成功地在我的angular站点中集成了Azure AD身份验证,按照msal-angular中的说明,现在我正在寻求定义和利用角色和权限,以提供对用户可以和不能做什么的更细粒度的控制。

根据我所能确定的,我可以通过遵循这组指令(https://docs.microsoft.com/en-us/azure/architecture/multitenant-identity/app-roles)来定义角色,但是msal-angular似乎不会在登录时公开这一点--或者至少我还没有找到关于如何做到这一点的指令。

也许我错过了什么。如有任何建议,我们将不胜感激。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2019-05-09 05:07:01

要获取用户所属的组,你需要将directory.read.all添加到你的Angular应用程序中的作用域以及Azure应用程序设置的API权限中。

代码语言:javascript
复制
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
    });
});
票数 3
EN

Stack Overflow用户

发布于 2019-05-08 20:55:21

(感谢stillatmylinux)

仅供参考:以下是我的angular 7解决方案(为了可读性起见进行了简化):

代码语言:javascript
复制
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');
      });
  });
}
票数 2
EN

Stack Overflow用户

发布于 2019-05-09 17:01:10

您可以更改清单以在令牌本身中传递这些内容。

sample (虽然是针对.NET的)详细解释了这一点

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56041125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档