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

时间:2016-05-30 15:57:34

标签: c# asp.net asp.net-core owin asp.net-core-mvc

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

目前使用Microsoft.Owin.Security.Cookies.Interop Configuration方法中过时的Startup.cs进行设置:

这很好用,但RC2不支持。

我们如何才能开始使用RC2的共享cookie身份验证?

1 个答案:

答案 0 :(得分:4)

结合https://github.com/GrabYourPitchforks/aspnet5-samples/tree/dev/CookieSharingSharing authentication cookie among Asp.Net Core 1 (MVC6) and MVC 5 applications我能够提出一个有效的解决方案。我不知道这是不是"正确"它的方法,但它的工作原理,所以它在这里:

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

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

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

  4. .NET 4.5.1,在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
            });
    

    Startup.cs的配置方法中的.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
                    });