How do you read Roles/Permissions using MSAL-ANGULAR

后端 未结 4 1683
天涯浪人
天涯浪人 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 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');
          });
      });
    }
    

提交回复
热议问题