Authentification par cookie partagé entre les applications ASP.NET Core RC2 et .NET 4.5.1

Nous avons deux applications .NET qui exécutent une authentification partagée par cookie. L’une est une application ASP.NET Core RC1 et l’autre est une application .NET 4.5.1 classique.

Ceci est actuellement configuré à l’aide de Microsoft.Owin.Security.Cookies.Interop obsolète dans la méthode de Configuration de Startup.cs :

Cela fonctionne bien, mais n’est pas une méthode prise en charge pour RC2.

Comment pouvons-nous démarrer avec l’authentification par cookie partagé pour RC2?

En combinant les cookies https://github.com/GrabYourPitchforks/aspnet5-samples/tree/dev/CookieSharing et Sharing entre les applications Asp.Net Core 1 (MVC6) et MVC 5, j’ai pu trouver une solution opérationnelle. Je ne sais pas si c’est la “bonne” façon de le faire, mais ça marche, alors voilà:

  1. Utilisez le package nuget Microsoft.Owin.Security.Interop 1.0.0-rc2-final dans les deux applications.

  2. Créez un TicketDataFormat utilisant DataProtectionProvider spécifiant le même emplacement sur le disque pour les clés de cryptage, ainsi que le même objective.

  3. Configurez l’authentification par cookie de la même manière dans les deux applications. Spécifiez les mêmes CookieName et TicketDataFormat :

.NET 4.5.1, dans la méthode de configuration de Startup.cs :

 var authenticationType = "Cookies"; var cookieName = "myCookieName"; var cookieEncryptionKeyPath= "C:/mypath"; var dataProtectionProvider = DataProtectionProvider.Create(new DirectoryInfo(cookieEncryptionKeyPath)); var dataProtector = dataProtectionProvider.CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", authenticationType, "v2"); var ticketDataFormat = new AspNetTicketDataFormat(new DataProtectorShim(dataProtector)); app.SetDefaultSignInAsAuthenticationType(authenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = authenticationType, CookieName = cookieName, TicketDataFormat = ticketDataFormat }); 

.NET CORE RC2 dans la méthode de configuration de Startup.cs :

 var authenticationType = "Cookies"; var cookieName = "myCookieName"; var cookieEncryptionKeyPath= "C:/mypath"; var protectionProvider = DataProtectionProvider.Create(new DirectoryInfo(cookieEncryptionKeyPath)); var dataProtector = protectionProvider.CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", authenticationType, "v2"); var ticketFormat = new TicketDataFormat(dataProtector); app.UseCookieAuthentication( new CookieAuthenticationOptions { CookieName = options.CookieName, CookieDomain = options.CookieDomain, TicketDataFormat = ticketFormat });