如何在身份验证后解决重定向时的 adal-angular4 无限循环



我正在构建一个 Angular 8 应用程序,该应用程序使用 adal-angular4 根据 Azure Active Directory 对用户进行身份验证Microsoft。我设置了一个 ASP.NET 核心 API,并将其与 Azure 上的客户端应用相关联。

我已按照以下文档设置活动目录:-

https://azure.microsoft.com/en-gb/resources/samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore/

以及以下内容,用于使用 adal-angular4 设置我的 Angular 应用程序和我的 .NET Core API:-

https://www.digital-moves.eu/2018/07/19/authentication-with-azure-ad-angular-6-client-web-api/

重定向 URL 似乎有效,因为我的应用程序中的正确回调组件被命中。但是,此时应用程序会进入重新尝试登录和重定向的无限循环。最终,登录尝试会产生异常"我们无法登录。请再试一次",大概是由于被锁定。

在我的应用程序中:-

providers: [
AdalService, { provide: HTTP_INTERCEPTORS, useClass: AdalInterceptor, multi: true },
RefreshTokenService

我的 adal 配置文件: -

private adalConfig = {
tenant: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
clientId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
redirectUri: 'http://localhost:4200/auth-callback',
postLogoutRedirectUri: "http://localhost:4200/end-session",
endpoints: { "https://visionapi2019.azurewebsites.net": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" },
cacheLocation: 'localStorage'
};
constructor(private adal: AdalService){
this.adal.init(this.adalConfig);
}
ngOnInit() {
this.adal.login();
}

在我的回调组件中,我有以下内容:-

ngOnInit() {
this.adal.handleWindowCallback();
this.router.navigate(['test-api']);
}

导航到组件"test-api"有效,但随后无限循环开始。

谢谢!

解决了这个问题。我在我的路由器中使用了[RouterModule.forRoot(routes,{ useHash:false })],并在app.component中从ngOnInit()中删除了this.adal.login() - 我想它是重新尝试登录每个重定向,然后由于URL中的哈希删除了id_token而失败。现在登录和重定向工作正常。

最新更新