IdentityServer 如何通过一次登录验证两个子域?

How to Authenticate two subdomain by one login in IdentityServer?

我有一个由 Duende IdentityServer 实施的 IDP 服务器假设它托管在 idp.com 上并且有两个单独的 ReactJS 应用程序托管在 app.mysite.comprofile.mysite.com 上并且它们使用 JWT 令牌用于身份验证和授权过程。现在,当我通过 idp.com 登录到 app.mysite.com 时,profile.mysite.com 未通过身份验证,需要再次登录。我对这两个站点使用相同的客户端配置。我知道有一些方法,例如在客户端代码中使用 IFRAME 在这两个应用程序之间共享 JWT 令牌,但我正在寻找身份服务器内部的内置方法来解决这个问题?

首先,如果你有2个客户端,你应该为它们分别配置2个单独的配置。

在第一次身份验证后,您应该依赖 idp.com 上设置的 cookie 来分离客户端。 (很高兴知道 - 如何设置 cookie 身份验证基本 cookie 身份验证:https://docs.microsoft.com/pl-pl/aspnet/core/security/authentication/cookie?view=aspnetcore-6.0

无论如何,如果您正确配置了 IdentityServer,它会处理 cookie 身份验证“out-of-the-box”——因此您可能唯一需要做的就是登录用户。

AuthenticationProperties props = new AuthenticationProperties
{
    IsPersistent = true,
    ExpiresUtc = DateTimeOffset.UtcNow.Add(LoginOptions.RememberMeLoginDuration)
};

var issuer = new IdentityServerUser(user.SubjectId)
{
    DisplayName = user.Username
};

await HttpContext.SignInAsync(issuer, props);

当用户想要登录第二个应用程序时,在流程开始(例如代码流程)并重定向到 idp.com 后,idp.com 知道用户已经 signed-in (cookie) 并应立即生成令牌并重定向回 return url.

如果需要,您可以使用 IProfileService 调整自定义行为。