我们有两个运行共享 cookie 身份验证的 .NET 应用程序。一个是 ASP.NET Core RC1 应用程序,另一个是经典的 .NET 4.5.1 应用程序。
目前这是使用过时的设置Microsoft.Owin.Security.Cookies.Interop
in the Configuration
的方法Startup.cs
:
该方法工作正常,但 RC2 不支持该方法。
我们如何开始 RC2 的共享 cookie 身份验证?
组合https://github.com/GrabYourPitchforks/aspnet5-samples/tree/dev/CookieSharing and 在 Asp.Net Core 1 (MVC6) 和 MVC 5 应用程序之间共享身份验证 cookie我能够想出一个可行的解决方案。我不知道这是否是“正确”的方法,但它有效,所以这里是:
使用 nuget 包Microsoft.Owin.Security.Interop 1.0.0-rc2-final
在这两个应用程序中。
创建一个TicketDataFormat
using DataProtectionProvider
为加密密钥指定磁盘上的相同位置以及相同的目的。
在两个应用程序中以 owin 方式配置 cookie 身份验证。指定相同CookieName
and TicketDataFormat
:
.NET 4.5.1中,在Configure方法中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
});
.NET CORE RC2中的Configure方法Startup.cs
:
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
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)