Featured

Sharing authentication cookies between applications

Last modified: June 03, 2022

1. Create a ASP.NET 6 application (e.g. MVC or Razor Page)

  • Make sure Authentication equals to Individual Account

auth

2. Database

  • Configure Appsettings.json
"ConnectionStrings": { "DefaultConnection": "Server=.;Database=user;Trusted_Connection=True;MultipleActiveResultSets=true" },
  • Run this command Update-Database on Package Manager Console

3. Program.cs

  • Configure as below, we need to have same cookie with the same key
builder.Services.AddDataProtection() .PersistKeysToFileSystem(new DirectoryInfo(@"c:\auth-key")) .SetApplicationName("SharedCookieApp"); builder.Services.ConfigureApplicationCookie(options => { options.Cookie.Name = ".AspNet.SharedCookie"; options.Cookie.Path = "/"; options.Cookie.Domain = "localhost"; });

4. Create a user

  • Run the application
  • Create a user by signing up

4. Create second Project

  • Repeat points from 1-3

5. Run both applications

auth

  • Login to App1 program
  • Once successful, go to App2 program, it should login automatically (SSO)
  • Once logout, it logout from both applications

auth

auth