Too many cookies OpenIdConnect.nonce cause error page “Bad Request - Request Too Long”

后端 未结 4 440
眼角桃花
眼角桃花 2020-12-02 19:15

I\'m using OWIN / OAuth with OpenId Connect authentication (Microsoft.Owin.Security.OpenIdConnect) in a C# ASP MVC web app. The SSO login with Microsoft account

相关标签:
4条回答
  • 2020-12-02 19:34

    OWIN and MVC may be deleting each other's cookies as described by the AspNetKatana github.

    • As a workaround that page suggests to explicitly use SystemWebCookieManager or SystemWebChunkingCookieManager (Microsoft.Owin.Host.SystemWeb 3.1.0).
    • There is also the Kentor Owin Cookie Saver as a workaround.
    • However, I would first try upgrading to Owin 4.1.0 (released november 2019), as this seems to fix it!
    0 讨论(0)
  • 2020-12-02 19:38

    For me the solution was to enforce the creation of an ASP.NET session.

    Steps to reproduce:

    • Delete ASP.NET_SessionId cookie and idsrv cookie on a protected page of your webapp
    • Reload page
    • Redirect to OIDC authentication store and authenticate
    • Redirect back to webapp => validation of the authentication fails, because no asp.net session is available
    • Endless redirects until 'Request too long...' error happens

    Solution: Enforce session creation by adding

    protected void Session_Start(object sender, EventArgs e)
    {
    }
    

    to global.asax.cs.

    0 讨论(0)
  • 2020-12-02 19:42

    In my case the problem was the order in which I was configuring the application inside Startup.cs.

    Reminder to self - always configure authentication first!

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = _clientId,
                    ClientSecret = _clientSecret,
                    Authority = _authority,
                    RedirectUri = _redirectUri
                });
    
            // configure the rest of the application...
        }
    
    0 讨论(0)
  • 2020-12-02 19:46

    It turned out that the root cause was the Ajax call.

    The problematic flow was

    1) OAuth cookie got expired after some time

    2) Expiration normally causes redirection the page to login.microsoft.com to refresh the cookie. In this step OAuth framework adds new nonce cookie to the response (every time)!

    3) But Ajax doesn't handle redirections outside of the domain (cross-domain to login.microsoft.com). But the cookie was already appended to the page.

    4) Next periodical Ajax call repeated the flow causing rapid increase of 'nonce' cookies.

    Solution

    I had to extend the "OWIN OpenId" framework setup code to handle Ajax calls differently - to prevent redirection and stop sending cookies.

    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
    
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    RedirectToIdentityProvider = ctx => 
                    {
                        bool isAjaxRequest = (ctx.Request.Headers != null && ctx.Request.Headers["X-Requested-With"] == "XMLHttpRequest");
    
                        if (isAjaxRequest)
                        {
                            ctx.Response.Headers.Remove("Set-Cookie");
                            ctx.State = NotificationResultState.HandledResponse;
                        }
    
                        return Task.FromResult(0);
                    }
                }
            });
    }
    

    The Ajax caller had to be adjusted too to detect 401 code and perform full page refresh (which caused a quick redirect to Microsoft authority).

    0 讨论(0)
提交回复
热议问题