当前位置: 首页 > 知识库问答 >
问题:

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

尚鸿才
2023-03-14

我有一个Asp.NET mvc应用程序,它连接到Identity server 4 Identity server。当我发布应用程序时,我发现了这个错误。

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

我跟踪到这个上游发送的标题太大,而从上游读取响应标题

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);
                    }
                };
            });

我一直在找,我似乎找不到一种方法来限制这个巨大的头球的大小。

共有1个答案

谢鸿飞
2023-03-14

默认情况下,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());
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);
    }
}
 类似资料:
  • 我正在尝试构建一个可以与azure web app通信的VSTS扩展。我能够做到这一点,但没有身份验证。我指的是Microsoft文档。 我获取了从浏览器即控制台生成的令牌。登录(令牌)并在此网站中验证。 上面写着无效的签名。 所提到的逻辑。Net framework验证生成的令牌无效。它给了我以下错误: IDX10500:签名验证失败。无法解析SecurityKeyIdentifier:“Sec

  • 我试图在PhantomJS中打开一个需要HTTP身份验证的网页。我的脚本基于loadspeed.js示例: 我可以从渲染的page.jpg中看到,我每次都得到401分。我还使用Wireshark跟踪了HTTP会话,这表明GET请求中没有向给定URL发送身份验证标头。 我到底做错了什么?我刚刚开始使用PhantomJS,但我整个晚上都在寻找,没有走远...

  • 我正在尝试在Dropwizard web应用程序中实现OAuth2身份验证。我已经创建了所需的<code>验证器 我所需的行为是,在我的客户端通过在我的登录页面上提供他/她的凭据登录后,我想将客户端重定向到我使用Dropwizard Views创建的问候语页面,并且路径为“/me”,如下所示: 我的问候资源如下所示: 目前,我得到一个“访问此资源需要凭据。”登录后的响应。在阅读了一些关于令牌认证的

  • 身份验证 PDF版下载 企业应用中的URL链接可以通过OAuth2.0验证接口来获取员工的身份信息。 通过此接口获取员工身份会有一定的时间开销。对于频繁获取员工身份的场景,建议采用如下方案: 企业应用中的URL链接直接填写企业自己的页面地址; 员工跳转到企业页面时,企业校验是否有代表员工身份的cookie,此cookie由企业生成; 如果没有获取到cookie,重定向到OAuth验证链接,获取员工

  • 我在REST api中使用JWT承载身份验证方案。为了在成功身份验证后将jwt令牌返回给客户端,目前我在正文中使用访问令牌响应,如中所述https://www.rfc-editor.org/rfc/rfc6750#page-10 但是也需要在其他HTTP请求中返回令牌,例如已经存在正文的注册。因此,正在考虑为其使用“身份验证信息”标头。但是承载方案没有在任何地方指定“身份验证信息”标头。我应该使用

  • 我试图将用于需要基本HTTP身份验证的第三方服务。我正在使用。以下是我到目前为止的结论: