-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Description
After many attempts to share authentication cookies without identity between ASP.NET 4.7.1 and .NET 5 hosted under IIS. Documentation is not clear, and lack key details with reference to configuring data protector for authentication cookie.
.NET Framework docs says that configure CookieAuthenticationOptions by setting TicketDataFormat where data protector has to be configured seen below. Docs should note that .NET 5 should also configure in exactly same way. See below
Note: Make sure purpose & sub purpose strings are same for .NET framework & .NET 5 set on Ticket Data Format.
//.NET Framework config
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
CookieName = ".AspNet.SharedCookie",
CookieSameSite = SameSiteMode.Lax,
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(120),
LoginPath = PathString.FromUriComponent("login path"),
LogoutPath = PathString.FromUriComponent("logout path"),
TicketDataFormat = new AspNetTicketDataFormat(
new DataProtectorShim(
DataProtectionProvider.Create(new DirectoryInfo("fileshare path") ,
(builder) =>
{
builder.SetApplicationName("iis-app-name");
})
.CreateProtector(
"Microsoft.AspNetCore.Authentication.Cookies." +
"CookieAuthenticationMiddleware",
"Cookies.Application",
"v2"))),
CookieManager = new ChunkingCookieManager()
});
//.NET 5 config
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.Name = ".AspNet.SharedCookie";
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.Path = "/";
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(120);
options.CookieManager = new ChunkingCookieManager();
options.TicketDataFormat = new SecureDataFormat<AuthenticationTicket>(new TicketSerializer(),
DataProtectionProvider.Create(new DirectoryInfo("fileshare path"),
(builder) => { builder.SetApplicationName("iis-app-name"); })
.CreateProtector(
"Microsoft.AspNetCore.Authentication.Cookies." +
"CookieAuthenticationMiddleware",
"Cookies.Application",
"v2"));
});
Every other variation I tried did not worked. It would be nice to update the docs with explicit instructions or link to working sample which provides both .NET framework 4.5.x app & .NET 5 app, which can be downloaded & hosted on IIS to see how its working. Randomly pointing to sample which has multiple startup's leaves so much to hunt for.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 99307104-c0d7-3e82-f0b2-af61dcfe072a
- Version Independent ID: 315c02cb-54c4-e386-da85-6e4311ca10c2
- Content: Share authentication cookies among ASP.NET apps
- Content Source: aspnetcore/security/cookie-sharing.md
- Product: aspnet-core
- Technology: aspnetcore-security
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande