簡體   English   中英

從 Angular 9 應用程序對 Azure AD 進行身份驗證

[英]Authenticate to Azure AD from Angular 9 Application

嘗試從我的 Angular 應用程序使用 Azure AD 進行身份驗證。 由於網絡上有太多過時的例子,很難理解如何做到這一點。 我一直在關注 github 上的最新文檔,但在訪問應用程序時我不斷收到此錯誤:

ClientConfigurationError: No redirect callbacks have been set. Please call handleRedirectCallback() with the appropriate function arguments before continuing

我該怎么做才能讓它發揮作用?

app-routing.module.ts

const routes: Routes = [
 { path: '', component: AppComponent, canActivate: [MsalGuard] },
 { path: 'auth-callback', component: DashboardComponent }
];

app.module.ts

const config = {
  auth: {
    clientId: 'my-client-id',
    //popUp: true,
    consentScopes: ['https://graph.microsoft.com/User.ReadWrite'],
    redirectUri: 'http://localhost:4200/auth-callback',
  }
};


@NgModule({
 declarations: [
 AppComponent,
 DashboardComponent
 ],
 imports: [
 MsalModule.forRoot(config),
 BrowserModule,
 AppRoutingModule
 ],
 providers: [
{
  provide: HTTP_INTERCEPTORS,
  useClass: MsalInterceptor,
  multi: true
}
]
,
bootstrap: [AppComponent]
})
export class AppModule {

constructor() {
  const myMSALObj = new UserAgentApplication(config);

   function authCallback(error, response) { }

    myMSALObj.handleRedirectCallback(authCallback);
 }

您需要添加回調,即使它什么都不做。 您可以將其添加到模塊構造函數中。

@NgModule({ ... })
export class AppModule {
  constructor(msalService: MsalService) {
    msalService.handleRedirectCallback(_ => { });
  }
}

不久前我寫了一篇關於這個的博客文章! Angular 9 破壞了我的身份驗證流程,所以必須弄清楚。

您可以在那里查看完整代碼以及您應該注意的所有細節。 https://www.pshul.com/2020/03/29/authenticate-your-angular-9-to-azure-ad-using-msal/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM