首页 > 解决方案 > 使用 IdentityServer4 进行子域多租户登录

问题描述

我正在尝试使用 identityserver4 实现多租户应用程序假设我有

当我登录到 web1.local.com 时,其他域 web2.local.com 也自动登录。

无论如何要分开这些登录名吗?

我正在考虑自定义实现IUserSession

public virtual async Task CreateSessionIdAsync(ClaimsPrincipal principal, AuthenticationProperties properties)
{
    if (principal == null) throw new ArgumentNullException(nameof(principal));
    if (properties == null) throw new ArgumentNullException(nameof(properties));

    var currentSubjectId = (await GetUserAsync())?.GetSubjectId();
    var newSubjectId = principal.GetSubjectId();

    if (!properties.Items.ContainsKey(SessionIdKey) || currentSubjectId != newSubjectId)
    {
        properties.Items[SessionIdKey] = CryptoRandom.CreateUniqueId(16);
    }

    IssueSessionIdCookie(properties.Items[SessionIdKey]);

    Principal = principal;
    Properties = properties;
}

private void IssueSessionIdCookie(string sid)
{
    if (Options.Endpoints.EnableCheckSessionEndpoint)
    {
        if (GetSessionIdCookieValue() != sid)
        {
            HttpContext.Response.Cookies.Append(
                Options.Authentication.CheckSessionCookieName,
                sid,
                CreateSessionIdCookieOptions());
        }
    }
}

最好的方法是什么?

标签: c#authenticationidentityserver4

解决方案


我相信您遇到的问题是,一旦 IdentityServer 发出会话 cookie,无论最初使用哪个应用程序登录,IdentityServer 将始终跳过来自任何其他应用程序的后续请求的登录(因为最初管理的会话 cookie) .

要始终强制不同应用程序之间的身份验证,您可以在授权请求上使用“提示”查询字符串并将其设置为“登录”。更多信息可以在这里找到:http ://docs.identityserver.io/en/latest/endpoints/authorize.html?highlight=prompt


推荐阅读