ASP.NET Core RC2和.NET 4.5.1应用程序之间的共享cookie身份validation

我们有两个运行共享cookie身份validation的.NET应用程序。 一个是ASP.NET Core RC1应用程序,另一个是经典的.NET 4.5.1应用程序。

这是使用Startup.csConfiguration方法中过时的Microsoft.Owin.Security.Cookies.Interop Configuration的:

这工作正常,但不支持RC2的方法。

我们如何才能使用RC2的共享cookie身份validation?

结合https://github.com/GrabYourPitchforks/aspnet5-samples/tree/dev/CookieSharing并在Asp.Net Core 1(MVC6)和MVC 5应用程序之间共享身份validationcookie,我能够提出一个可行的解决方案。 我不知道这是否是“正确”的方法,但它确实有效,所以这里有:

  1. 在两个应用程序中使用nuget-package Microsoft.Owin.Security.Interop 1.0.0-rc2-final

  2. 使用DataProtectionProvider创建TicketDataFormat ,为加密密钥指定磁盘上的相同位置,以及相同的目的。

  3. 在两个应用程序中以owin方式配置cookie身份validation。 指定相同的CookieNameTicketDataFormat

.NET 4.5.1,在Startup.cs的Configure方法中:

 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 }); 

Startup.cs的Configure方法中的.NET CORE RC2:

 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 });