[英]Shared cookie authentication between ASP.NET Core RC2 and .NET 4.5.1 apps
我們有兩個運行共享cookie身份驗證的.NET應用程序。 一個是ASP.NET Core RC1應用程序,另一個是經典的.NET 4.5.1應用程序。
這是使用Startup.cs
的Configuration
方法中過時的Microsoft.Owin.Security.Cookies.Interop
Configuration
的:
這工作正常,但不支持RC2的方法。
我們如何才能使用RC2的共享cookie身份驗證?
結合https://github.com/GrabYourPitchforks/aspnet5-samples/tree/dev/CookieSharing並在Asp.Net Core 1(MVC6)和MVC 5應用程序之間共享身份驗證cookie,我能夠提出一個可行的解決方案。 我不知道這是否是“正確”的方法,但它確實有效,所以在這里:
在兩個應用程序中使用nuget-package Microsoft.Owin.Security.Interop 1.0.0-rc2-final
。
使用DataProtectionProvider
創建TicketDataFormat
,為加密密鑰指定磁盤上的相同位置,以及相同的目的。
在兩個應用程序中以owin方式配置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
});
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
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.