首页 文章

在两个ASP.NET核心应用程序之间共享Cookie

提问于
浏览
1

我在两个单独的ASP.NET Core项目中使用WsFederation .

每个项目在 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:

enter image description here

然后我导航到同一子域上的项目#2 . 但是,项目#2无法识别 MySharedCookie 并重新进行身份验证 . 我得到一个名称相同但值不同的新cookie:

enter image description here

我正在尝试在ASP.NET Core中做什么?在ASP.NET Core中有没有办法我可以与项目#2共享项目#1的cookie?

1 回答

  • 3

    这记录在Sharing cookies among apps with ASP.NET and ASP.NET Core . 还有一个Cookie Sharing App Sample可用 .

    为了共享cookie,您需要在每个应用程序中创建 DataProtectionProvider 并在应用程序之间使用公共/共享密钥集 .

    .AddCookie(options =>
    {
        options.Cookie.Name = ".SharedCookie";
        options.Cookie.Domain = "example.com";
        options.DataProtectionProvider =
            DataProtectionProvider.Create(new DirectoryInfo("path-tokeys"));
    });
    

相关问题