我在 Azure 中有一个租户,它托管许多应用程序。我正在使用 OpenID Connect 进行授权。在 login.microsoft.com openid-configuration 中,提供了一个 end_session_endpoint
URL。 This本文介绍了如何发送注销请求。这将结束 Azure 中的 session ,但不会结束我的客户端(Angular2 SPA)上的 session 。为此,我使用 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-aaaaaaaaaaaa/oauth2/logout?post_logout_redirect_uri=https%3a%2f%2fsitename.azurewebsites.net%2fHome%2fSignedOut
post_logout_redirect_uri
参数允许您指定用户的浏览器在注销后应重定向到何处。这样用户就不会丢失上下文。请确保在重定向之前在您的应用端保存用户的工作,这样您就不会激怒用户。
关于javascript - Angular 应用程序中 Azure AD OpenID Connect 注销的 CORS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44286427/