My ASP.NET site was integrated successfully with Auth0, and followed the code sample here: ASP.NET (OWIN) - Auth0 Docs . Login & logout worked correctly. At that time it was on .NET Fmk 4.7.2 & using Microsoft.Owin.X nugets version 4.2.2 and Microsoft.IdentityModel.X nugets version 5.7.0.
I then upgraded it to run under .NET Fmk 4.8 and updated Microsoft.Owin.X nugets to version 4.2.3 and Microsoft.IdentityModel.X nugets to version 8.4.0.
At this point the login flow stopped working … the site would redirect to Auth0, authentication completed OK in Auth0 and returned the user to the site, the site handled the redirect, read the claims etc, and attempted to log the user in (by setting a Forms Authentication ticket cookie). However the Forms Authentication cookie was dropped by the browser (see below) and the user was not logged in, leading them to be redirected back to Auth0 and back in an endless loop.
I figured out a solution, and think I understand why it works, but not sure if I have thrown out something valuable in the process.
In the original code, OWIN was configured to use cookie authentication as below (similar to here: ASP.NET (OWIN) - Auth0 Docs ):
app.SetDefaultSignInAsAuthenticationType(Constants.AUTH_TYPE);
app.UseCookieAuthentication(
new CookieAuthenticationOptions
{
AuthenticationType = Constants.AUTH_TYPE,
CookieName = Constants.AUTH_COOKIE_NAME,
});
The CookieName was set to the same name as the site’s Forms Authentication ticket cookie.
This worked OK, but after the upgrade I noticed that in the redirect from Auth0, there were 2 set-cookie headers for this cookie, then one would be dropped. I think one was set by OWIN, and one was set by my code to log the user into the site.
So basically the problem was that now there are 2 systems issuing cookies with the same name, in the same response, with different semantics. The browser kept only one, and not the one the site recognised for its authentication.
I played around for a long time with the SameSite settings and different CookieManager classes, but in the end the only thing that worked was to remove the app.UseCookieAuthentication call entirely. This stopped the duplicate cookies being issued, and the login & logout functionality are restored.
My only questions are - what was the purpose of the app.UseCookieAuthentication call as it is recommended in the documentation? Have I lost something, or is there no other solution if using Forms Authentication in the site?
Many thanks for any insight.