首页 > 解决方案 > 使用 IdentityServer4 的 SSO 是否应该在每个请求上点击授权端点?

问题描述

我正在使用 IdentityServer4 从多个 ASP.net Core 网站实现单点登录。我能够通过一个站点登录,并且成功地将我登录到另一个站点。然而,我觉得奇怪的是,在我登录到两个站点后,如果我同时浏览了另一个站点,任何需要身份验证/授权的页面都会将我重定向回授权端点。一个例子:

+-----+--------------------+-------------------------------------+---------------------------------------------------------------------+
| Seq |        Host        |               Request               |                              Response                               |
+-----+--------------------+-------------------------------------+---------------------------------------------------------------------+
|   1 | apple.example.com  | GET /About                          | 302 Found; Location=https://login.example.com/connect/authorize?... |
|   2 | login.example.com  | GET /connect/authorize?...          | 302 Found; Location=https://login.example.com/Account/Login?…       |
|   3 | login.example.com  | GET Account/Login?…                 | 200 OK                                                              |
|   4 | login.example.com  | POST /Account/Login?...             | 302 Found; Location=/connect/authorize/callback?...                 |
|   5 | login.example.com  | GET /connect/authorize/callback?... | 200 OK                                                              |
|   6 | apple.example.com  | POST /signin-oidc                   | 302 Found; Location=https://apple.example.com/About                 |
|   7 | apple.example.com  | GET /About                          | 200 OK                                                              |
|   8 | banana.example.com | GET /About                          | 302 Found; Location=https://login.example.com/connect/authorize?... |
|   9 | login.example.com  | GET /connect/authorize?...          | 200 OK                                                              |
|  10 | banana.example.com | POST /signin-oidc                   | 302 Found; Location=https://banana.example.com/About                |
|  11 | banana.example.com | GET /About                          | 200 OK                                                              |
|  12 | banana.example.com | GET /About                          | 200 OK                                                              |
|  13 | banana.example.com | GET /About                          | 200 OK                                                              |
|  14 | apple.example.com  | GET /About                          | 302 Found; Location=https://login.example.com/connect/authorize?... |
|  15 | login.example.com  | GET /connect/authorize?...          | 200 OK                                                              |
|  16 | apple.example.com  | POST /signin-oidc                   | 302 Found; Location=https://apple.example.com/About                 |
|  17 | apple.example.com  | GET /About                          | 200 OK                                                              |
|  18 | apple.example.com  | GET /About                          | 200 OK                                                              |
+-----+--------------------+-------------------------------------+---------------------------------------------------------------------+

直到 Seq=11 一切都按预期工作。我登录了两个站点(苹果和香蕉),但只输入了一次我的凭据。我上次在banana.example.com 上加载了一个页面。只要我留在那个站点(第 12 行和第 13 行),站点 cookie 就可以工作。但是,如果我在 apple.example.com 上加载另一个页面,它会将我传递回授权端点,即使我已经在该站点上通过了身份验证。这让我很惊讶。这种情况继续发生——每当我切换站点时,我都必须再次访问身份服务器。我不必重新输入我的凭据,但重定向有点刺耳。我特别担心它会干扰 POST 请求。

我们的网站是这样的,用户同时使用多个网站,来回切换是很常见的。

这是它应该工作的方式,还是我的配置有问题?我希望每个站点只需要访问一次授权端点。

标签: asp.net-coreidentityserver4

解决方案


你的预期是正确的。确保您为每个应用程序使用不同的 cookie 名称,否则它会导致您描述的确切行为。

您可以在身份验证服务配置中设置 cookie 名称:

services.AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; })
        .AddCookie(options =>
        {
            options.Cookie.Name = "someCookieName";
        })

推荐阅读