Angular 应用程序中 Azure AD OpenID Connect 注销的 CORS 问题

CORS issue with Azure AD OpenID Connect logout in Angular app

我在 Azure 中有一个租户,它托管了许多应用程序。我正在使用 OpenID Connect 进行授权。在 login.microsoft.com openid-configuration 中,提供了 end_session_endpoint URL。 This article explains how to send a sign-out request. This will end the session in Azure, but not on my client (an Angular2 SPA). To do this, I'm using angular-oauth2-oidc 和以下代码:

this.http.get('https://login.microsoftonline.com/TENANT_ID/oauth2/logout",')
    .toPromise()
    .then(function () { 
        this.oauthService.logOut();
    }
);

对 login.microsoftonline.com 的 GET 将在服务器上结束 session,而 logOut() 调用将在本地结束 session。当我 运行 这样做时,出现以下错误:

XMLHttpRequest cannot load https://login.microsoftonline.com/TENANT_ID/oauth2/logout. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:51518' is therefore not allowed access.

当我检查来自 login.microsoftonline.com GET 的响应时,Access-Control-Allow-Origin 不是响应 header 之一。我很惊讶它不会有 header 和 * 作为价值。无论如何,我该如何解决这个问题?我看到应用服务中有一个 CORS 设置,我将我的本地主机(目前在本地测试)添加到租户中的一个应用程序的 CORS,但它仍然无法正常工作。

暂时,我在我的应用程序 API 上添加了一个 API 端点,它在 C# 中调用 login.microsoftonline.com URL,但它很烦人要做到这一点。我想通过 javascript.

完成这一切

您需要将浏览器重定向到 URL。你不能调用它 AJAX.

You must also redirect the user to the end_session_endpoint for sign-out.

Azure AD 不允许在登录终结点上进行跨域调用,您无法更改它。

您可以将用户重定向到:

https://login.microsoftonline.com/ccb87602-82bf-4f35-b7d2-aa‌​aaaaaaaaaa/oauth2/lo‌​gout?post_logout_red‌​irect_uri=https%3a%2‌​f%2fsitename.azurewe‌​bsites.net%2fHome%2f‌​SignedOut

post_logout_redirect_uri 参数允许您指定用户的浏览器在注销后应该重定向到哪里。这样用户就不会丢失上下文。确保在重定向之前在您的应用端保存用户的工作,以免激怒用户。