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

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

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

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

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

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

最佳答案

合并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. 使用 DataProtectionProvider 创建一个 TicketDataFormat,为加密 key 在磁盘上指定相同的位置,目的相同。

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

关于c# - ASP.NET Core RC2 和 .NET 4.5.1 应用程序之间的共享 cookie 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37529741/

相关文章:

c# - 使用 system.data.sqlclient.dll 安装客户端

c# - 在 ASP.net C# 中,有没有一种方法可以将控制权从 Javascript 传递给我的 CS?

linux - 停止在 kestrel 上运行的正在运行的 dotnet 核心网站

Azure Function 2.x - 获取当前用户的声明

c# - 返回相对图像路径的 html 帮助程序未显示图像,但在 View 中硬编码相同路径时显示

c# - 事务 Rollback() 是否有机会抛出异常?

C#.NET 3.5 : How to invoke an event handler and wait for it to complete

跨子域的 Asp.Net 配置文件

javascript - 拖放表行 (asp.net mvc)

c# - 具有应用程序见解的依赖关系日志记录不会记录响应