Enable ASP.NET Core 2 cookies to read old ASP.NET Forms Authentication cookies

FormsAuthentication

Enable ASP.NET Core 2 cookies to read old ASP.NET Forms Authentication cookies by implementing a custom ISecureDataFormat.

NuGet: NuGet Shield

Usage:

var section = Configuration.GetSection("FormsAuthentication");

var faOptions = new FormsAuthenticationOptions()
{
    DecryptionKey = section.GetValue("DecryptionKey"),
    ValidationKey = section.GetValue("ValidationKey"),
    EncryptionMethod = section.GetValue("EncryptionMethod"),
    ValidationMethod = section.GetValue("ValidationMethod"),
};

services
    .AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
    .AddCookie(options =>
    {
        options.Cookie.Name = section.GetValue("CookieName");
        options.AccessDeniedPath = "/Login/Upgrade/";
        options.LoginPath = "/Login/";
        options.ReturnUrlParameter = "returnurl";
        options.TicketDataFormat = new FormsAuthenticationDataFormat(
            faOptions,
            FormsAuthHelper.ConvertCookieToTicket,
            FormsAuthHelper.ConvertTicketToCookie
            );
    });

The FormsAuthHelper.ConvertCookieToTicket and FormsAuthHelper.ConvertTicketToCookie helper methods convert an ASP.NET Core AuthenticationTicket to a FormsAuthenticationCookie and vise versa. This class contains the same data as a old ASPNET Cookie.

Please refer to errornoerror.com for more similar questions