How do you read Roles/Permissions using MSAL-ANGULAR

后端 未结 4 1650
天涯浪人
天涯浪人 2021-01-06 11:44

So I\'ve successfully integrated Azure AD authentication in my angular site as per the instructions in msal-angular and now I\'m at the point where I\'m looking to define an

相关标签:
4条回答
  • 2021-01-06 11:52

    To get the groups a user belongs to, you will need to add directory.read.all to your scope in your Angular app and also in the API permissions in the Azure app settings.

    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
        });
    });
    
    0 讨论(0)
  • 2021-01-06 12:05

    (Thanks goes to stillatmylinux)

    FYI: Here's my working angular 7 solution (simplified for the sake of readability):

    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');
          });
      });
    }
    
    0 讨论(0)
  • 2021-01-06 12:10

    You can change the manifest to get these delivered in the token itself.

    The sample (its for .NET though), explains this in detail

    0 讨论(0)
  • 2021-01-06 12:13

    You only needs User.Read permission and to use memberof v1. Use import * as MicrosoftGraph from '@microsoft/microsoft-graph-client'; to fix microsoft-graph-client header export bug. uniqueId is your AzureAD user id.

    private loginSuccess(uniqueId: string) {
         console.log('LOGIN SUCCESS ', uniqueId);
         (this.graphClient = MicrosoftGraph.Client.init({
          authProvider: async (done) => {
            let param: AuthenticationParameters = { authority:"https://login.microsoftonline.com/{TenantId}",
                                                  scopes:["User.Read"]
                          };
            let response = await this.authService.acquireTokenSilent(param)
            .catch((reason) => {
              done(reason, null);
              });
    
            if (response) {
              done(null, response.accessToken);
            } else {
              done("Could not get an access token", null);
            }
          }
        })).api(`/users/${uniqueId}/memberOf`).get()
        .then((response)=>{
            this.groups = response.value;
            console.log("members ok", this.groups);
        },
        (error)=>{
          console.error('memberOf error');
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题