【发布时间】:2022-01-07 10:51:56
【问题描述】:
我将我的项目更新为 MSAL2.0 angular。登录,注销和警卫工作正常。它不会向后端 api 的任何请求添加任何不记名令牌。 MSAL 文档说它会自动添加到请求中。
我正在使用 @azure/msal-angular: "^2.0.5", @azure/msal-browser: "^2.16.1"
以下是我的代码 - app.module.ts
import { msalConfig } from './Shared/azure-config';
export function MSALInstanceFactory(): IPublicClientApplication {
return new PublicClientApplication(msalConfig);
}
/**
* Set your default interaction type for MSALGuard here. If you have any
* additional scopes you want the user to consent upon login, add them here as well.
*/
export function MSALGuardConfigFactory(): MsalGuardConfiguration {
return {
interactionType: InteractionType.Redirect,
};
}
export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration {
const protectedResourceMap = new Map([
[protectedResources.MainStreetContact.endpoint,protectedResources.MainStreetContact.scopes]
])
return {
interactionType: InteractionType.Redirect,
protectedResourceMap
};
}
@NgModule({
declarations: [
AppComponent
],
imports: [
HttpClientModule,
BrowserModule,
UserIdleModule.forRoot({idle: 3600, timeout: 30, ping: 120}),
AppRoutingModule,
BrowserAnimationsModule,
MsalModule
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: MsalInterceptor,
multi: true,
},
{
provide: MSAL_INSTANCE,
useFactory: MSALInstanceFactory
},
{
provide: MSAL_GUARD_CONFIG,
useFactory: MSALGuardConfigFactory
},
{
provide: MSAL_INTERCEPTOR_CONFIG,
useFactory: MSALInterceptorConfigFactory
},
MsalService,
MsalGuard,
MsalBroadcastService
],
bootstrap: [AppComponent]
})
export class AppModule { }
azure-config -
export const protectedResources = {
MainStreetContact: {
endpoint: "https://app.mainstreetcontact.com/",
scopes: ['access_as_user'],
},
}
【问题讨论】: