title: Correlation failed. at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler during OIDC authentication
body: <p>I am hitting this with the following combination:</p>
  <ol>
  <li>Browser incognito mode (Chrome)</li>
  <li>Application is behind Azure application gateway (no repro if it isn't). Cookie based affinity is turned OFF (default); if turned ON, seems to make repro happen more often.</li>
  </ol>
  <p>Code is rather plain vanilla OIDC authN + cookies.</p>
  <pre><code>        services.AddAuthentication(options =&gt;
          {
              options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
              options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
          })
          .AddAzureAd(options =&gt; {
              Configuration.Bind(&quot;AzureAd&quot;, options);
          })
          .AddCookie(p =&gt; p.SlidingExpiration = true);
  </code></pre>
  <p>I am forwarding the X-Forwarded-Proto header to the auth middleware as recommended so the redirect_uri uses the correct protocol scheme.</p>
  <p><strong>HANDLING IN CODE</strong></p>
  <p>I tried to handle the <strong>OnRemoteFailure()</strong> event, and redirect to &quot;/Home/AuthRedirect&quot; which is an anon page that waits for 20 secs, and then redirects to the &quot;/&quot; (home page). It seems to work sometimes, but not always. I am out of ideas.</p>
  <p><strong>WORKAROUND</strong></p>
  <ol>
  <li>Users can go to the homepage again and hit F5 until this works. It seems that each F5 gets them moving a step ahead and once the OpenID cookies are populated, everything else (I have more auth after openid finishes, via adal.js for AJAX use).</li>
  <li>Bypass the application gateway and use the direct service fabric cluster DNS name (not acceptable as it is http).</li>
  </ol>
  <p><strong>DETAILS</strong></p>
  <pre>
  System.Exception: Correlation failed.
     at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.d__12.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.d__6.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()
  </pre>
  <p><img src="https://user-images.githubusercontent.com/9466367/39440060-907a5506-4c5e-11e8-8208-3cfa7dc9847c.png" alt="image" /></p>