首页 文章

c#Identity Server错误请求 - 请求太长

提问于
浏览
1

我有一个奇怪的问题,我试图追查 .

如果我使用自签名证书将我的客户端和Identity Server部署到Azure,则代码可以正常工作 .

我现在已将其移至我们的UAT环境,其中身份服务器配置为使用购买的证书 . 此证书是为单个域提供的 . identity.mydomain.com

客户端具有此证书的密码,因此它可以执行所需的操作 .

当我浏览到身份服务器时,我可以登录管理部分,这样就可以正常运行了 . 如果我浏览到客户端,它会重定向到我可以登录的身份服务 . 但是一旦我登录并重定向回我的网站,我就会收到以下错误;

Bad Request - Request Too Long

HTTP Error 400. The size of the request headers is too long.

看着cookie,我可以看到创建了大量的cookie . 我删除了那些并重新启动,但我仍然有同样的问题 . 如果我通过使用增加缓冲区的大小 .

<httpRuntime maxRequestLength="2097151" executionTimeout="2097151">

然后它工作,但我担心我正在掩盖一个问题,而不是修复它 .

有没有其他人必须这样做才能让身份服务器在iis上工作?

2 回答

  • 2

    我最近遇到过这个问题 . 解决方案是降级使用过的NuGet包 Microsoft.Owin.Security.OpenIdConnect . 我使用的是3.0.1 . 您必须降级到3.0.0 . 这是Owin / Katana中间件的问题 . 可以在here找到该问题的描述 . 请注意,该页面说明了如何修复库中的实际问题 . 我没有尝试过,它也可以工作,值得一试 .

    请注意,首次使用修补程序重新部署时,必须清除Cookie . 作为临时修复,您可以随时清除Cookie,然后再次访问该网站 . 但是,在某些时候,它总是会在cookie中粘贴一堆nonce字符串 . 类似的问题可以在here找到 .

  • 1

    是什么解决了我的问题是使用AdamDotNet's Custom OpenIdConnectAuthenticationHandler删除旧的nonce cookie .

    public static class OpenIdConnectAuthenticationPatchedMiddlewareExtension
        {
            public static Owin.IAppBuilder UseOpenIdConnectAuthenticationPatched(this Owin.IAppBuilder app, Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions openIdConnectOptions)
            {
                if (app == null)
                {
                    throw new System.ArgumentNullException("app");
                }
                if (openIdConnectOptions == null)
                {
                    throw new System.ArgumentNullException("openIdConnectOptions");
                }
                System.Type type = typeof(OpenIdConnectAuthenticationPatchedMiddleware);
                object[] objArray = new object[] { app, openIdConnectOptions };
                return app.Use(type, objArray);
            }
        }
    
        /// <summary>
        /// Patched to fix the issue with too many nonce cookies described here: https://github.com/IdentityServer/IdentityServer3/issues/1124
        /// Deletes all nonce cookies that weren't the current one
        /// </summary>
        public class OpenIdConnectAuthenticationPatchedMiddleware  : OpenIdConnectAuthenticationMiddleware
        {
            private readonly Microsoft.Owin.Logging.ILogger _logger;
    
            public OpenIdConnectAuthenticationPatchedMiddleware(Microsoft.Owin.OwinMiddleware next, Owin.IAppBuilder app, Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions options) 
                    : base(next, app, options)
            {
                this._logger = Microsoft.Owin.Logging.AppBuilderLoggerExtensions.CreateLogger<OpenIdConnectAuthenticationPatchedMiddleware>(app);
            }
    
            protected override Microsoft.Owin.Security.Infrastructure.AuthenticationHandler<OpenIdConnectAuthenticationOptions> CreateHandler()
            {
                return new SawtoothOpenIdConnectAuthenticationHandler(_logger);
            }
    
            public class SawtoothOpenIdConnectAuthenticationHandler : OpenIdConnectAuthenticationHandler
            {
                public SawtoothOpenIdConnectAuthenticationHandler(Microsoft.Owin.Logging.ILogger logger)
                    : base(logger) { }
    
                protected override void RememberNonce(OpenIdConnectMessage message, string nonce)
                {
                    var oldNonces = Request.Cookies.Where(kvp => kvp.Key.StartsWith(OpenIdConnectAuthenticationDefaults.CookiePrefix + "nonce"));
                    if (oldNonces.Any())
                    {
                        Microsoft.Owin.CookieOptions cookieOptions = new Microsoft.Owin.CookieOptions
                        {
                            HttpOnly = true,
                            Secure = Request.IsSecure
                        };
                        foreach (KeyValuePair<string, string> oldNonce in oldNonces)
                        {
                            Response.Cookies.Delete(oldNonce.Key, cookieOptions);
                        }
                    }
                    base.RememberNonce(message, nonce);
                }
            }
        }
    

    并使用:

    app.UseOpenIdConnectAuthenticationPatched(new OpenIdConnectAuthenticationOptions(){...});
    

    详情如下:https://github.com/IdentityServer/IdentityServer3/issues/1124#issuecomment-226519073

相关问题