Exception: Correlation failed.
This exception keeps occurring while working on a web application using ASP.NET Core. Just to add a little background, I am doing authentication configured using Azure Ad of our web app. However, the authentication and authorization are working absolutely alright, the correlation issue occurring intermittently. There is no specific trend on occurrence but, it mostly occurs while idle the application for a while.
In order to fix it, I have tried multiple tweaks to my code, First of all, I have tried to change the CookieSecurePolicy to secure.
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
However, This doesn’t work for me. it still reduces the chance of occurring the exception.
At last, I’ve found a hook on the middleware to handle this error. On the authenticating client application where the OpenID Connect middleware is configured, I’ve put the below code:
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
options.RemoteAuthenticationTimeout = TimeSpan.FromSeconds(10);
options.Events.OnRemoteFailure = RemoteAuthFail;
});private Task RemoteAuthFail(RemoteFailureContext context) { context.Response.Redirect("/Home/AuthError"); context.HandleResponse(); return Task.CompletedTask; }
I’ve put a friendly message on that page prompting them, not to bookmark the login (as well as on the login screen).
This actually solves the correlation issue for me. Please find the link to the discussions
Hope this helps!