【问题标题】:Shared cookie authentication between ASP.NET Core RC2 and .NET 4.5.1 appsASP.NET Core RC2 和 .NET 4.5.1 应用程序之间的共享 cookie 身份验证
【发布时间】:2016-05-30 15:57:33
【问题描述】:

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

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

这很好用,但不支持 RC2 的方法。

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

【问题讨论】:

  • 我希望你能解决这个问题。我同样需要将 RC2 中的 cookie 传递到 4.6.1。

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


【解决方案1】:

结合https://github.com/GrabYourPitchforks/aspnet5-samples/tree/dev/CookieSharing 和Sharing 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. 使用DataProtectionProvider 创建一个TicketDataFormat,指定加密密钥在磁盘上的相同位置以及相同的用途。

  3. 在两个应用程序中以自己的方式配置 cookie 身份验证。指定相同的CookieName 和TicketDataFormat:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2019-08-17
    • 1970-01-01
    • 2018-03-09
    • 2021-03-20
    • 2019-07-05
    相关资源
    最近更新 更多