I am attempting to share my authentication Cookies between a ASP.NET Core application and a ASP.NET 4.X application.
I am following this tutorial: Share authentication cookies among ASP.NET apps . However I am stuck because our .NET Core application uses the PersistKeysToDbContext
to share the Cookie keys:
services.AddDataProtection()
.SetApplicationName("MyApplicationName")
.PersistKeysToDbContext<MyDatabaseContext>();
And, as per the tutorial, I will use the following code in my .NET 4.6 application:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Identity.Application",
CookieName = ".AspNet.SharedCookie",
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity =
SecurityStampValidator
.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) =>
user.GenerateUserIdentityAsync(manager))
},
TicketDataFormat = new AspNetTicketDataFormat(
new DataProtectorShim(
DataProtectionProvider.Create("{PATH TO COMMON KEY RING FOLDER}",
(builder) => { builder.SetApplicationName("SharedCookieApp"); })
.CreateProtector(
"Microsoft.AspNetCore.Authentication.Cookies." +
"CookieAuthenticationMiddleware",
"Identity.Application",
"v2"))),
CookieManager = new ChunkingCookieManager()
});
The DataProtectionProvider.Create
does not seem to allow for either database storage or for custom storage. I am completely stuck - how do I use SQL server to share the keys from .NET Core to .NET 4.6?
In the end I did find the solution. I implemented my own version of PersistKeysToDbContext
. Now the code for "TicketDataFormat" (in Startup.Auth.cs) looks like this:
TicketDataFormat = new AspNetTicketDataFormat(
new DataProtectorShim(
DataProtectionProvider.Create(
new DirectoryInfo("{PATH TO COMMON KEY RING FOLDER}"),
(builder) => {
builder.SetApplicationName("SharedCookieApp");
builder.Services.AddSingleton<IConfigureOptions<KeyManagementOptions>>(services =>
{
return new ConfigureOptions<KeyManagementOptions>(options =>
{
options.XmlRepository = new MyXmlRepository();
});
});
}).CreateProtector(
"Microsoft.AspNetCore.Authentication.Cookies." +
"CookieAuthenticationMiddleware",
"Cookies",
"v2"))),
You must now implement a class that reads and writes to the database, which must implement the interface Microsoft.AspNetCore.DataProtection.Repositories.IXmlRepository
:
public class MyXmlRepository : IXmlRepository
{
public virtual IReadOnlyCollection<XElement> GetAllElements()
{
// This function must return a list of all the elements in the database
}
public void StoreElement(XElement element, string friendlyName)
{
// This function must write to the database (this is optional if this
// service never provides the authentication, only uses it)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.