マルチテナント

既定では、MSAL は、構成で指定されていない場合に、権限内のテナントを "共通" に設定するため、アプリケーションのマルチテナント サポートがあります。これにより、すべてのMicrosoft アカウントがアプリケーションに対して認証できるようになります。 マルチテナントの動作に関心がない場合は、次に示すように、authorityで MSAL をインスタンス化するときに、app.module.ts構成プロパティを設定する必要があります。

@NgModule({
  imports: [
    MsalModule.forRoot({ // MSAL Configuration
      auth: {
        clientId: 'CLIENT_ID_HERE',
        authority: 'https://login.microsoftonline.com/TENANT_ID_HERE',
        redirectUri: 'http://localhost:4200',
        postLogoutRedirectUri: 'http://localhost:4200'
      },
      // Additional configuration here
    });
  ]
})
export class AppModule {}

マルチテナント認証を許可し、すべてのMicrosoft アカウントユーザーにアプリケーションの使用を許可しない場合は、ログインが許可されているテナントのみにトークン発行者をフィルター処理する独自の方法を指定する必要があります。

テナントの変更

次に示すように、関連するコンポーネントで MSAL の新しいインスタンスをインスタンス化することで、テナントを動的に設定することもできます。

import { PublicClientApplication } from '@azure/msal-browser';
import { MsalService } from '@azure/msal-angular';

@Component({})
export class AppComponent implements OnInit {
  constructor(
    private authService: MsalService
  ) {}

  ngOnInit(): void {
    this.authService.instance = new PublicClientApplication({
      auth: {
        clientId: 'CLIENT_ID_HERE',
        authority: 'https://login.microsoftonline.com/TENANT_ID_HERE',
        redirectUri: 'http://localhost:4200',
        postLogoutRedirectUri: 'http://localhost:4200'
      }
    });
  }

動的認証リクエスト

既定では、MsalGuard と MsalInterceptor は構成で設定された静的プロパティを使用します。どちらも、認証に使用されるパラメーターを動的に変更できるように、 authRequestのメソッドを使用して構成することもできます。

MsalInterceptor - 動的認証要求 (マルチテナント トークン)

organizationsまたはcommonがテナントとして使用されている場合は、すべてのトークンがユーザーのホーム テナントに対して要求されます。 ただし、これは望ましい結果ではない可能性があります。 ユーザーがゲストとして招待された場合、トークンは間違った機関から取得されている可能性があります。

authRequestをメソッドに設定すると、認証要求を動的に変更できます。 たとえば、ゲスト ユーザーを使用するときに、アカウントのホーム テナントに基づいて権限を設定できます。 authRequestのプロパティは変更できますが、常に次のようにoriginalAuthRequestを拡張する必要があります。

export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
  const protectedResourceMap = new Map<string, Array<string>>();
  protectedResourceMap.set("https://graph.microsoft.com/v1.0/me", ["user.read"]);
  
  return {
    interactionType: InteractionType.Popup,
    protectedResourceMap,
    authRequest: (msalService, httpReq, originalAuthRequest) => {
      return {
        ...originalAuthRequest,
        authority: `https://login.microsoftonline.com/${originalAuthRequest.account?.tenantId ?? 'organizations'}`
      };
    }
  };
}
...

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
    ...
    {
      provide: MSAL_INTERCEPTOR_CONFIG,
      useFactory: MSALInterceptorConfigFactory
    }
  ]
});