Enable ASP.NET Core 2 cookies to read old ASP.NET Forms Authentication cookies by implementing a custom ISecureDataFormat
.
Usage:
var section = Configuration.GetSection("FormsAuthentication");
var faOptions = new FormsAuthenticationOptions()
{
DecryptionKey = section.GetValue<string>("DecryptionKey"),
ValidationKey = section.GetValue<string>("ValidationKey"),
EncryptionMethod = section.GetValue<EncryptionMethod>("EncryptionMethod"),
ValidationMethod = section.GetValue<ValidationMethod>("ValidationMethod"),
};
services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.Name = section.GetValue<string>("CookieName");
options.AccessDeniedPath = "/Login/Upgrade/";
options.LoginPath = "/Login/";
options.ReturnUrlParameter = "returnurl";
options.TicketDataFormat = new FormsAuthenticationDataFormat<AuthenticationTicket>(
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.