By default authentication tickets cannot be shared between .NET Core and OWIN. The good news is that it is possible but we have to take some extra steps:
.NET Core App
On .NET Core side we have to change the cookie authentication middleware:
- The cookie name should match the name used by the OWIN Cookie Authentication Middleware (
.AspNet.SharedCookie
for example). - An instance of a
DataProtectionProvider
should be initialized to the common data protection key storage location.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
var protectionProvider = DataProtectionProvider.Create( | |
new DirectoryInfo(@"c:\temp\keyring")); | |
var dataProtector = protectionProvider.CreateProtector( | |
"CookieAuthenticationMiddleware", | |
"Cookie", | |
"v2"); | |
var ticketFormat = new TicketDataFormat(dataProtector); | |
services.AddAuthentication("Federation") | |
.AddCookie("Federation",options => | |
{ | |
options.Cookie.Name = ".AspNet.SharedCookie"; | |
options.TicketDataFormat = ticketFormat; | |
}); | |
services.AddControllersWithViews(); | |
} |
ASP.NET (OWIN) App
On ASP.NET (OWIN) side we have to install the Microsoft.Owin.Security.Interop package first.
Then we can change the cookie authentication middleware:
- The cookie name should match the name used by the ASP.NET Core Cookie Authentication Middleware (
.AspNet.SharedCookie
in the example). - An instance of a
DataProtectionProvider
should be initialized to the common data protection key storage location.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureAuth(IAppBuilder app) | |
{ | |
var protectionProvider = DataProtectionProvider.Create( | |
new DirectoryInfo(@"c:\temp\keyring")); | |
var dataProtector = protectionProvider.CreateProtector( | |
"CookieAuthenticationMiddleware", | |
"Cookie", | |
"v2"); | |
var ticketFormat = new AspNetTicketDataFormat(new DataProtectorShim(dataProtector)); | |
app.UseCookieAuthentication( | |
new CookieAuthenticationOptions | |
{ | |
AuthenticationType = | |
WsFederationAuthenticationDefaults.AuthenticationType, | |
CookieName= ".AspNet.SharedCookie", | |
TicketDataFormat = ticketFormat | |
}); | |
} |