在两个ASP.NET Core应用程序之间共享Cookie [英] Sharing Cookies Between Two ASP.NET Core Applications
问题描述
我在两个独立的ASP.NET中使用 WsFederation 核心项目.
I'm using WsFederation in two separate ASP.NET Core projects.
每个项目在Startup.cs
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(options =>
{
options.Wtrealm = Configuration["Wtrealm"];
options.MetadataAddress = "http://example.com/metadata.xml";
options.SkipUnrecognizedRequests = true;
options.RequireHttpsMetadata = false;
options.UseTokenLifetime = false;
})
.AddCookie(options =>
{
options.Cookie.Name = "MySharedCookie";
options.Cookie.Path = "/";
options.Cookie.Domain = ".dev.example.com";
});
我在浏览器中加载了项目#1,并且得到了我的cookie:
I load project #1 in the browser and I get my cookie:
然后,我导航到同一子域上的项目#2.但是,项目2无法识别MySharedCookie
并重新进行身份验证.我得到一个同名但值不同的新Cookie:
I then navigate to project #2 on the same sub domain. However, project #2 doesn't recognize MySharedCookie
and re-authenticates. I get a new cookie with the same name but a different value:
我正在ASP.NET Core中尝试做些什么吗?在ASP.NET Core中,我可以与项目#2共享项目1的cookie吗?
Is what I'm trying to do possible in ASP.NET Core? Is there a way in ASP.NET Core I can share project #1's cookie with project #2?
推荐答案
为了共享cookie,请在每个应用程序中创建一个DataProtectionProvider
,并在应用程序之间使用一组公用/共享的密钥.
In order to share cookies, you create a DataProtectionProvider
in each app and using a common/shared set of keys between the apps.
.AddCookie(options =>
{
options.Cookie.Name = ".SharedCookie";
options.Cookie.Domain = "example.com";
options.DataProtectionProvider =
DataProtectionProvider.Create(new DirectoryInfo("path-tokeys"));
});
这篇关于在两个ASP.NET Core应用程序之间共享Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!