I have recently switched to a different workstation and on the old workstation my login cookies/session would persist after I rebuild the project so when debugging something I wouldn’t need to always relogin after tweaking the code and rebuilding the solution.
Now on the new workstation, each time I rebuild, I get redirected to the login page of my project and need to relogin. I can see the cookies are still there when I check through Chrome’s dev console but somehow the application ignores them.
Here’s some info about the 2 workstations: old machine was a Win Server 2022 and new machine is Windows 11) and I’m virtually using the same IDE configuration VS 2022, imported the same VS settings, pulled the project from Git, etc. I checked every configuration file I could (IIS Express, Web.config, etc.
The project uses Microsoft.Owin.Security.Cookies and the initialization code looks like this:
public class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
Owin.AppBuilderExtensions.CreatePerOwinContext<ApplicationDbContext>(app, new Func<ApplicationDbContext>(ApplicationDbContext.Create));
Owin.AppBuilderExtensions.CreatePerOwinContext<ApplicationRoleManager>(app, new Func<IdentityFactoryOptions<ApplicationRoleManager>, IOwinContext, ApplicationRoleManager>(ApplicationRoleManager.Create));
Owin.AppBuilderExtensions.CreatePerOwinContext<ApplicationUserManager>(app, new Func<IdentityFactoryOptions<ApplicationUserManager>, IOwinContext, ApplicationUserManager>(ApplicationUserManager.Create));
Owin.AppBuilderExtensions.CreatePerOwinContext<ApplicationSignInManager>(app, new Func<IdentityFactoryOptions<ApplicationSignInManager>, IOwinContext, ApplicationSignInManager>(ApplicationSignInManager.Create));
IAppBuilder app1 = app;
CookieAuthenticationOptions options = new CookieAuthenticationOptions();
string str = "ApplicationCookie";
options.AuthenticationType = str;
PathString pathString = new PathString("/Account/Login");
options.LoginPath = pathString;
options.Provider = (ICookieAuthenticationProvider)new CookieAuthenticationProvider()
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(TimeSpan.FromMinutes(30.0), (Func<ApplicationUserManager, ApplicationUser, Task<ClaimsIdentity>>)((manager, user) => user.GenerateUserIdentityAsync((UserManager<ApplicationUser>)manager)))
};
CookieAuthenticationExtensions.UseCookieAuthentication(app1, options);
Owin.AppBuilderExtensions.UseExternalSignInCookie(app, "ExternalCookie");
Owin.AppBuilderExtensions.UseTwoFactorSignInCookie(app, "TwoFactorCookie", TimeSpan.FromMinutes(5.0));
Owin.AppBuilderExtensions.UseTwoFactorRememberBrowserCookie(app, "TwoFactorRememberBrowser");
}
public void Configuration(IAppBuilder app)
{
InitApp.Init();
this.ConfigureAuth(app);
}
}
The Owin package I use is:
<package id="Microsoft.Owin.Security.Cookies" version="4.2.0" targetFramework="net472" />
as you can see the project uses ASP.NET MVC 4.7.2. The project works well everywhere else, it’s been in production for years to thousands of people with no complains so I doubt it’s about the code but more about my configuration.
I’ve been scratching my head for 24h to figure that small problem. I searched as much as I can, found nothing similar on here or anywhere else. So my last resort is so post here hoping that somebody encountered something similar at some point in their career.
Do not hesitate to ask for more details. Thanks!