Skip to content

Share authentication cookies between ASP.NET 4.x and ASP.NET Core apps #21987

@marafiq

Description

@marafiq

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions