Azure active directory loadDiscoveryDocumentAndTryLogin()的CORS阻止错误
我正在从Azure B2C进行Angular 9身份验证。在angular中,我使用的是Auth angular-oauth2-oidc库 如果我在config方法中加载了discoverydocumentandtrylogin()代码,并且不确定我缺少了什么,那么我将得到CORS块错误。如果我禁用LoadDiscoveryDocumentandTryLogging()代码,那么我将设法重定向到Azure B2C登录页面Azure active directory loadDiscoveryDocumentAndTryLogin()的CORS阻止错误,azure-active-directory,angular9,angular-oauth2-oidc,Azure Active Directory,Angular9,Angular Oauth2 Oidc,我正在从Azure B2C进行Angular 9身份验证。在angular中,我使用的是Auth angular-oauth2-oidc库 如果我在config方法中加载了discoverydocumentandtrylogin()代码,并且不确定我缺少了什么,那么我将得到CORS块错误。如果我禁用LoadDiscoveryDocumentandTryLogging()代码,那么我将设法重定向到Azure B2C登录页面 import{OnInit,Component,Injector}来自“
import{OnInit,Component,Injector}来自“@angular/core”;
从'angular-oauth2-oidc'导入{OAuthService};
从'angular-oauth2-oidc-jwks'导入{JwksValidationHandler};
从“../../config/auth.config”导入{authConfig};
@组成部分({
选择器:“应用程序验证登录页”,
templateUrl:'auth landing.component.html',
样式URL:['auth-landing.component.scss']
})
导出类AuthComponent实现OnInit{
_accessToken:字符串;
_idToken:字符串;
构造函数(专用注入器:注入器){}
私有获取oauthService(){
返回此.injector.get(OAuthService)
}
专用异步配置身份验证():承诺{
this.oauthService.configure(authConfig);
this.oauthService.tokenValidationHandler=新的JwksValidationHandler();
this.oauthService.loadDiscoveryDocumentAndTryLogin();
this.oauthService.setStorage(sessionStorage);
}
异步ngOnInit(){
等待此消息。ConfigureAuth();
}
异步登录(){
this.oauthService.tryLogin({});
如果(!this.oauthService.getAccessToken()){
等待这个.oauthService.initImplicitFlow();
}
this.u accessToken=this.oauthService.getAccessToken();
this.idToken=this.oauthService.getIdToken();
设validToken=this.oauthService.hasValidAccessToken();
console.log(此._accessToken);
}
注销(){
this.oauthService.logOut();
}
代币(){
let claims:any=this.oauthService.getIdentityClaims();
退货索赔?索赔:空;
}
readToken()
{
log(“ID令牌”,this.\u accessToken);
log(“访问令牌”,this.\u idToken);
let claims:any=this.oauthService.getIdentityClaims();
控制台日志(索赔);
}
}
格式不正确。在OAuthService配置中使用以下各项:
loginUrl:“https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/{policy}/oauth2/v2.0/authorize”,
发行人:“https://{tenant}.b2clogin.com/{tenant}.onmicrosoft.com/{policy}/v2.0/”,
此外,与问题无关但值得一提的是:
您没有在此函数中等待任何内容:
private async ConfigureAuth():Promise{
和/或更重要的是,避免async ngOnInit
不要发布代码、数据、错误消息等的图像-将文本复制或键入问题。
import { OnInit, Component, Injector } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks';
import { authConfig } from '../../config/auth.config';
@Component({
selector: 'app-auth-landing-page',
templateUrl: 'auth-landing.component.html',
styleUrls: ['auth-landing.component.scss']
})
export class AuthComponent implements OnInit{
_accessToken: string;
_idToken: string;
constructor(private injector: Injector) { }
private get oauthService() {
return this.injector.get(OAuthService)
}
private async ConfigureAuth(): Promise<void>{
this.oauthService.configure(authConfig);
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
this.oauthService.loadDiscoveryDocumentAndTryLogin();
this.oauthService.setStorage(sessionStorage);
}
async ngOnInit(){
await this.ConfigureAuth();
}
async login(){
this.oauthService.tryLogin({});
if(!this.oauthService.getAccessToken()){
await this.oauthService.initImplicitFlow();
}
this._accessToken = this.oauthService.getAccessToken();
this._idToken = this.oauthService.getIdToken();
let validToken = this.oauthService.hasValidAccessToken();
console.log(this._accessToken);
}
logout(){
this.oauthService.logOut();
}
token(){
let claims: any = this.oauthService.getIdentityClaims();
return claims ? claims : null;
}
readToken()
{
console.log("ID Token ", this._accessToken );
console.log("Access Token", this._idToken);
let claims: any = this.oauthService.getIdentityClaims();
console.log(claims);
}
}