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

时间:2016-12-02 14:44:53

标签: c# ssl identityserver3

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

如果我使用自签名证书将我的客户端和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上运行?

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

为我解决问题的是使用AdamDotNet's Custom OpenIdConnectAuthenticationHandler删除旧的nonce cookie。

(String) field;

并使用:

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

如此处详细说明: https://github.com/IdentityServer/IdentityServer3/issues/1124#issuecomment-226519073

答案 2 :(得分:0)

清除cookie对我有用。首先尝试是最简单的答案。

相关问题