Home > Blockchain >  Authentication cookie sharing between ASP WebForms and ASP.NET Core MVC app
Authentication cookie sharing between ASP WebForms and ASP.NET Core MVC app

Time:10-11

I have 2 ASP.NET applications, one is ASP.NET WebForms on .NET 4.5, and the other in an ASP.NET Core 6 app.

The connection of the user is done on the application in WebForms. When the user is connected, I have to redirect him to the ASP.NET Core application and it must automatically identify the user who has connected.

I use an authentication cookie on the webforms application with FormsAuthenticationTicket.

Is it possible to share this cookie between the 2 applications?

If I ask you this question it's because I'm stuck.

I found some resources about sharing the authentication cookie between ASP.NET MVC and Core applications but I can't find anything for Webforms applications

Thanks for your help !

CodePudding user response:

You can share the cookie between the asp.net application and asp.net core application. The data protection stack allows sharing Katana cookie authentication and ASP.NET Core cookie authentication tickets.

You need add code like below:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddAuthentication();
    ...
}

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    DataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\"))
});

And in your webform application.

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = "Cookies",
    CookieName = ".AspNetCore.Cookies",
    // CookiePath = "...", (if necessary)
    // ...
    TicketDataFormat = new AspNetTicketDataFormat(
        new DataProtectorShim(
            DataProtectionProvider.Create(new DirectoryInfo(@"c:\shared-auth-ticket-keys\"))
            .CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware",
            "Cookies", "v2")))
});

For more details you can check the link I provided.

And you also can find the refer the webform repo.

  • Related