我将msal用于angular 8,并将.NET core 2.2用于webapi。我从我的有角度的应用程序创建令牌,并想验证webapi。 我已经在Azure门户中注册了我的两个应用程序(角度和本地webapi),并且还公开了api。 以下是我的app.module.ts文件内容:
let msalConfig: MsalConfig;
export function initializeApp(appConfig: AppConfig) {
const promise = appConfig.loadAppConfig().pipe(tap((settings: IAppConfig) => {
const protectedResourceMap:[string, string[]][]=
[
['http://localhost:7308/api/values',['api://{azure-web-api-app-clientID}/values']],
];
msalConfig = {
authority: 'https://login.microsoftonline.com/{azure-tenantId}',
clientID: 'azure-angular-api-app-clientID',
protectedResourceMap: protectedResourceMap,
validateAuthority: true,
isAngular:true,
piiLoggingEnabled:true,
cacheLocation : "localStorage",
redirectUri: "http://localhost:8081/",
storeAuthStateInCookie: true
};
})).toPromise();
return () => promise;
}
export function msalConfigFactory() {
return msalConfig;
}
以下是app.module.ts中的提供程序
providers: [
ProductService,
{
provide: APP_INITIALIZER,
useFactory: initializeApp,
deps: [AppConfig],
multi: true
},
MsalService,
{
provide: MSAL_CONFIG,
useFactory: msalConfigFactory
},
{
provide: HTTP_INTERCEPTORS,
useClass: MsalInterceptor,
multi: true
}
]
对于Asp.net核心2.2 webapi startup.cs文件
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("EnableCors", builder =>
{
builder.WithOrigins("http://localhost:8081").AllowAnyHeader().AllowAnyMethod();
});
});
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = AzureADDefaults.BearerAuthenticationScheme;
})
.AddAzureADBearer(options => Configuration.Bind("AzureActiveDirectory", options));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
IdentityModelEventSource.ShowPII = true;
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseCors("EnableCors");
app.UseAuthentication();
app.UseStaticFiles();
app.UseMvc();
}
以及在appsettings.json
中"AzureActiveDirectory": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "{my-domain}.onmicrosoft.com",
"TenantId": "{azure-web-app-api-tenantid}",
"ClientId": "azure-web-api-app-clientID"
}
当我尝试调用此api时,出现500错误。它甚至都没有指向api(/ api / values)。
确切的错误是
IDX20803:无法从以下位置获取配置:“ https://login.microsoftonline.com/ {my-tennantId} /。well-known / openid-configuration”