ASP.NET Core RC2和.NET 4.5.1应用之间的共享cookie身份验证

4
我们有两个运行共享cookie身份验证的.NET应用程序。一个是ASP.NET Core RC1应用程序,另一个是传统的.NET 4.5.1应用程序。
目前,这是使用过时的Microsoft.Owin.Security.Cookies.Interop在Startup.cs的Configuration方法中设置的:
现在这样做可以工作,但不支持RC2。
我们如何开始使用RC2进行共享cookie身份验证呢?

我希望你能解决这个问题。我和你一样,需要将RC2的Cookies传递到4.6.1中。 - Matthew Verstraete
1个回答

4

结合 https://github.com/GrabYourPitchforks/aspnet5-samples/tree/dev/CookieSharingSharing authentication cookie among Asp.Net Core 1 (MVC6) and MVC 5 applications,我成功地找到了一个可行的解决方案。我不知道这是否是“正确”的方法,但它起作用了,所以就这样吧:

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

  2. 使用 DataProtectionProvider 创建一个 TicketDataFormat,指定相同的加密密钥位置和相同的目的。

  3. 在两个应用程序中使用 OWIN 的方式配置 cookie 认证。指定相同的 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
                });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接