【问题标题】:c# Identity Server Bad Request - Request Too Longc# Identity Server 错误请求 - 请求太长
【发布时间】:2020-12-09 23:34:41
【问题描述】:

我有一个奇怪的问题要追查。

如果我使用自签名证书将我的客户端和身份服务器部署到 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 上工作?

【问题讨论】:

  • 我刚刚搜索了错误...support.microsoft.com/en-us/kb/2020943我在该页面上看到了解决方案。似乎与证书没有任何关系。
  • 我已经看到了,谢谢,我们没有使用 kerberos 或 AD。奇怪的是,使用完全干净的机器,我可以按预期登录。尽管已经在此问题上工作了一两天的其他机器无法正常工作。在做了更多阅读之后,我倾向于认为这可能是一个 cookie 问题。

标签: c# ssl identityserver3


【解决方案1】:

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

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

【讨论】:

  • 谢谢,我试试这个。
  • 虽然奇​​怪的是它间歇性地工作。一个用户总是可以让它在 chrome 中工作,但永远不能 ie。
  • 我假设Microsoft.Owin.Security.OpenIdConnect Nuget 包您指的是在客户端应用程序中?我的客户端应用程序是在具有 1.1.2 运行时的 ASP.NET Core 中开发的。我遇到了同样的问题
【解决方案2】:

为我解决问题的是使用 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

【讨论】:

  • 对我来说,它没有帮助,它只是继续重定向,而不是每次抛出错误请求 - 请求太长错误:(((((
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2018-08-09
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2018-02-16
    • 2021-07-01
    • 2019-01-09
    • 2018-10-06
    相关资源
    最近更新 更多