My issue is that I would like to share the ASP.NET identity cookie between .NET Core and .NET
I have the latest version of ASP.NET Identity in both places - the .NET Core is a new login page, the .NET is a legacy app that will be converted to .NET Core in the distant future.
I would like the two apps to share the cookie so if you log out of one, it logs out of the other.
Has anyone any experience of this? Know what settings are needed? Surely its something that has come up somewhere before?
This is my code: ASP.NET Core (.NET 6) (login page)
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"c:\temp\common"))
//.ProtectKeysWithCertificate("thumbprint")
.SetApplicationName("SharedCookieApp");
builder.Services.ConfigureApplicationCookie(options =>
{
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
options.SlidingExpiration = true;
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.Name = ".MyCookie";
});
ASP.NET 4.8 (legacy app)
public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Identity.Application",
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
},
// TODO: Need to make the Dataprotection work on Azure
CookieName = ".AspNet.SharedCookie",
CookieSameSite = Microsoft.Owin.SameSiteMode.Lax,
CookieSecure = CookieSecureOption.Always,
TicketDataFormat = new AspNetTicketDataFormat(
new DataProtectorShim(
DataProtectionProvider.Create(new DirectoryInfo(@"c:\temp\common"),
builder => builder.SetApplicationName("SharedCookieApp"))//.ProtectKeysWithCertificate("thumbprint") // for production
.CreateProtector(
"Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware",
// Must match the Scheme name used in the ASP.NET Core app, i.e. IdentityConstants.ApplicationScheme
"Identity.Application",
"v2"))),
CookieManager = new Microsoft.Owin.Infrastructure.ChunkingCookieManager()
});
);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
I have created a github repo with what I think is working code. I would very much appreciate any comments as there doesn't appear to be an example on the web that I could find anyway.