Hi,
I try to share 2 authentication cookies between 2 applications.
In App1 : Startup.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookies", LoginPath = new PathString("/Account/Login"), AccessDeniedPath = new PathString("/Home/Forbidden"), AutomaticAuthenticate = true, AutomaticChallenge = true, CookieName = "ASPTest", ExpireTimeSpan = new TimeSpan(1, 0, 0) //1 hour });
And in App2 : Startup.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookies", LoginPath = new PathString("/Account/Login"), AccessDeniedPath = new PathString("/Home/Forbidden"), AutomaticAuthenticate = true, AutomaticChallenge = true, CookieName = "ASPTest", });
In Ap1 : AccountController.cs
[HttpGet] public IActionResult Login(string returnUrl) { ViewData["ReturnUrl"] = returnUrl; return View(); } [HttpPost] public async Task<IActionResult> Login(string username, string password, string returnUrl) { if (username == password) { var claims = new List<Claim> { new Claim("Read", "true"), new Claim(ClaimTypes.Name, "ayayalar"), new Claim(ClaimTypes.Sid, "12345") }; var claimsIdentity = new ClaimsIdentity(claims, "password"); var claimsPrinciple = new ClaimsPrincipal(claimsIdentity); await HttpContext.Authentication.SignInAsync("Cookies", claimsPrinciple, new Microsoft.AspNetCore.Http.Authentication.AuthenticationProperties { IsPersistent = true}); if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } return Redirect("~/"); } return View(); }
In both Ap1 and Ap2 : I check after logged in (tested without database and login == password)
public IActionResult Index() { var t = HttpContext.User; return View(); }
In Ap1 which allows to login and create cookie, the cookie is created and when going on Home/Index the t variable is feed from Cookie==> it works!
In Ap2 when going on Home/Index the t variable is not feed from Cookie ==> it fails !
In Mozilla I'm able to see the cookie created within both App1 and App2, but App2 seems not able to load it.
I tried to follow the link : https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/compatibility/cookie-sharing but with no success as
DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\"))
does not build.
Did I miss some obvious point when trying to get access the cookie created from Ap1 with Ap2 ? My goal is to create a cookie from Ap1, and get all Claims from Ap2.
Thank you for your help
Nicolas