身份验证限制扩展标头大小

DaI*_*mTo 5 c# header asp.net-core-mvc identityserver4

我有一个连接到 Identity server 4 身份服务器的 Asp .net mvc 应用程序。当我发布该应用程序时,我对这个错误感到困惑。

从上游读取响应标头时上游发送了太大的标头

我跟踪到的上游在从上游读取响应标头时发送了太大的标头

我无法更改配置,系统管理员已声明我们需要缩小标头。

在此输入图像描述

看完之后我不得不承认这些标题有点广泛。

应用程序中的Startup.cs

services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
            {

                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.Authority = Configuration["ServiceSettings:IdentityServerEndpoint"];
                options.RequireHttpsMetadata = true;
                options.ClientId = Configuration["ServiceSettings:ClientId"];
                options.ClientSecret = Configuration["ServiceSettings:secret"];
                options.Scope.Add("testapi");
                options.ResponseType = "code id_token";
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;
                options.Events = new OpenIdConnectEvents()
                {
                    OnRemoteFailure = ctx =>
                    {
                        _logger.LogCritical($"Remote Faliure: {ctx.Failure}");
                        ctx.HandleResponse();
                        return Task.FromResult(0);
                    }
                };
            });
Run Code Online (Sandbox Code Playgroud)

我一直在寻找,但似乎找不到一种方法来限制这个巨大标题的大小。

Evk*_*Evk 6

默认情况下,cookie 包含所有加密的相关信息,因此当调用您的 api 时 - 您所需要做的就是解密 cookie 并使用该信息。

然而,将所有内容存储在 cookie 本身中通常是不可取的,尤其是在有大量相关信息的情况下。Cookie 会随每个请求发送到您的 api,如果它很大,则大量(相同)信息基本上会无缘无故地来回发送。另外,正如您所见,cookie 的大小可能会成为某些环境中的限制因素。

因此,您可以将该信息存储在其他地方(例如服务器内存中),而不是在 cookie 本身中发送所有信息,并且仅将该信息的标识符放在 cookie 本身中。

您可以像这样配置会话存储:

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(o => o.SessionStore = new MemoryCacheTicketStore());
Run Code Online (Sandbox Code Playgroud)

MemoryCacheTicketStore您可以在 github 上的asp.net示例中找到示例实现:

public class MemoryCacheTicketStore : ITicketStore
{
    private const string KeyPrefix = "AuthSessionStore-";
    private IMemoryCache _cache;

    public MemoryCacheTicketStore()
    {
        _cache = new MemoryCache(new MemoryCacheOptions());
    }

    public async Task<string> StoreAsync(AuthenticationTicket ticket)
    {
        var guid = Guid.NewGuid();
        var key = KeyPrefix + guid.ToString();
        await RenewAsync(key, ticket);
        return key;
    }

    public Task RenewAsync(string key, AuthenticationTicket ticket)
    {
        var options = new MemoryCacheEntryOptions();
        var expiresUtc = ticket.Properties.ExpiresUtc;
        if (expiresUtc.HasValue)
        {
            options.SetAbsoluteExpiration(expiresUtc.Value);
        }
        options.SetSlidingExpiration(TimeSpan.FromHours(1)); // TODO: configurable.

        _cache.Set(key, ticket, options);

        return Task.FromResult(0);
    }

    public Task<AuthenticationTicket> RetrieveAsync(string key)
    {
        AuthenticationTicket ticket;
        _cache.TryGetValue(key, out ticket);
        return Task.FromResult(ticket);
    }

    public Task RemoveAsync(string key)
    {
        _cache.Remove(key);
        return Task.FromResult(0);
    }
}
Run Code Online (Sandbox Code Playgroud)