首页 > 解决方案 > IdentityServer3 idsrv.partial cookie 变得太大

问题描述

登录后使用部分 cookie 重定向用户时context.AuthenticateResult = new AuthenticateResult(<destination>, subject, name, claims)变得如此之大以至于它包含多达 4 个块并最终导致“请求太大”错误。

索赔的数量并不惊人(在 100 范围内),即使索赔数量较多,我也无法在其他环境中始终如一地重现这一点。还有什么可能会影响此 cookie 有效负载的大小?

运行 IdSrv3 2.6.1

标签: identityserver3

解决方案


我假设您正在使用一些 .NET Framework 客户端,因为所有这些问题通常都与Microsoft.Owin中间件有关,中间件有一些加密会导致 cookie 变得这么大。

您的解决方案又是该中间件的一部分。您的所有客户端(使用身份服务器作为授权)都需要自定义IAuthenticationSessionStore实现。

这是一个接口,是Microsoft.Owin.Security.Cookies.

您需要根据您想要使用的任何商店来实现它,但基本上它具有以下结构:

public interface IAuthenticationSessionStore
{
    Task RemoveAsync(string key);
    Task RenewAsync(string key, AuthenticationTicket ticket);
    Task<AuthenticationTicket> RetrieveAsync(string key);
    Task<string> StoreAsync(AuthenticationTicket ticket);
}

我们最终为 cookie 实现了一个 SQL Server 存储。这是Redis Implementation的一些示例,这是EF DbContext的其他示例,但不要觉得被迫使用其中任何一个。

假设您MyAuthenticationSessionStore : IAuthenticationSessionStore使用它需要的所有值来实现。

然后在你的 OwinStartup.cs调用时:

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies",
            SessionStore = new MyAuthenticationSessionStore()
            CookieName = cookieName
        });

由此,正如该IAuthenticationSessionStore SessionStore物业的文件所述:

// 一个可选容器,用于跨请求存储身份。使用时, // 仅向客户端发送会话标识符。这可以用来缓解 // 非常大的身份的潜在问题。

在您的标头中,您将只有会话标识符和身份本身,将从您已实现的 Store 中读取


推荐阅读