c# Identity Server Bad Request - Request Too Long

后端 未结 3 2123
萌比男神i
萌比男神i 2021-01-03 07:03

I have an odd issue that I am trying to track down.

If I deploy my client and Identity Server to Azure, using a self signed certificate then the code works.

3条回答
  •  旧巷少年郎
    2021-01-03 07:41

    What solved the problem for me was using AdamDotNet's Custom OpenIdConnectAuthenticationHandler to delete old nonce cookies.

    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);
            }
        }
    
        /// 
        /// 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
        /// 
        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(app);
            }
    
            protected override Microsoft.Owin.Security.Infrastructure.AuthenticationHandler 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 oldNonce in oldNonces)
                        {
                            Response.Cookies.Delete(oldNonce.Key, cookieOptions);
                        }
                    }
                    base.RememberNonce(message, nonce);
                }
            }
        }
    

    And use:

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

    As detailed here: https://github.com/IdentityServer/IdentityServer3/issues/1124#issuecomment-226519073

提交回复
热议问题