Search code examples
c#asp.netasp.net-coreauthenticationcookies

Sharing cookie authentication from ASP.NET 5+ application to legacy ASP.NET 4.x


I searched and found some examples mentioning the sharing of authentication cookies between .NET Framework and .NET Core/5+ applications, but most of citations are of cookies being generated in the older version of .NET being reused in Core, not the opposite that is what I need.

Share Cookie authentication between ASP.NET Core 2.2 and ASP. NET MVC 5 (.NET Framework 4.6.1) without Microsoft.Identity

share authentication cookie between .NET Framework and .Net Core

https://learn.microsoft.com/en-us/aspnet/core/security/cookie-sharing?view=aspnetcore-7.0#share-authentication-cookies-between-aspnet-4x-and-aspnet-core-apps

The closest case was this one.

I need my .NET Framework 4.7.2 website to be able to use the authentication of my .NET Core Website

But the version of reference is seems .NET Core 2.1 and some methods in the answer has been deprecated and I couldn't reproduce it. Also seems the authentication being performed in older application.

How could I setup a web app in ASP.NET 7 with individual accounts authentication and reuse this cookie to authorize other applications (legacy 4.x web apps modules, and also newer applications that'll be implemented)?


Solution

  • The middleware UseCookieAuthentication is obsolete. Configure Cookie authentication with AddAuthentication().AddCookie When you regist the service ,and call app.UseAuthentication() middleware,Here's the document related

    You could try as below in your .net core project and follow the case you provided

        builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(x =>
            {
                
                x.DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(.......);
            }
            );
    
    .....
    app.UseAuthentication()
    ......