我在我的 angular 8 应用程序中使用了Damien Bod 的 angular-auth-oidc-client和“新”Azure B2C 端点:
- https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/oauth2/v2.0/authorize
- https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/oauth2/v2.0/token
STS 服务器看起来像这样:
- https://{租户}.b2clogin.com/tfp/{租户}/B2C_1_SuSi_v2/oauth2/v2.0/
但问题是 oidc lib 向https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/oauth2/v2.0/token?p=b2c_1_susi_v2
我得到CORS错误:
从源“ https://192.168.3.2:4200 ”访问“https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/oauth2/v2.0/token?p=b2c_1_susi_v2”处的 XMLHttpRequest已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。
我在这里做错了什么?这是带有 PKCE 的代码流。
这是我的App.module.ts的核心:
export function loadConfig(oidcConfigService: OidcConfigService, httpClient: HttpClient) {
if (!environment.production) {
console.log("APP_INITIALIZER STARTING");
}
return () =>
httpClient
.get(`${window.location.origin}/api/oidc`)
.pipe(
take(1),
switchMap((config: OidcConfig) => of(config)),
tap(config => {
oidcConfig = config;
}),
map(
config =>
`https://${config.tenant}.b2clogin.com/${
config.tenant
}.onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1_SuSi_v2`
)
)
.toPromise()
.then(wellKnownUri => oidcConfigService.load_using_custom_stsServer(wellKnownUri));
}
export class AppModule {
constructor(
private oidcSecurityService: OidcSecurityService,
private oidcConfigService: OidcConfigService
) {
this.oidcConfigService.onConfigurationLoaded.subscribe((configResult: ConfigResult) => {
// Use the configResult to set the configurations
const config: OpenIdConfiguration = {
stsServer: configResult.customConfig.stsServer,
redirect_url: oidcConfig.redirect_url,
client_id: oidcConfig.client_id,
scope: oidcConfig.scope, // "code",
response_type: oidcConfig.response_type,
post_logout_redirect_uri: oidcConfig.post_logout_redirect_uri,
silent_renew: true,
silent_renew_url: "/silent-renew.html",
post_login_route: oidcConfig.post_login_route,
forbidden_route: oidcConfig.forbidden_route,
unauthorized_route: oidcConfig.unauthorized_route,
auto_userinfo: oidcConfig.auto_userinfo,
log_console_debug_active: !environment.production
// all other properties you want to set
};
this.oidcSecurityService.setupModule(config, configResult.authWellknownEndpoints);
});
if (!environment.production) {
console.log("APP STARTING");
}
}
}