如何在ASP.NET Core中的子域之间共享会话?

Tec*_*chy 9 c# cookies middleware asp.net-core-mvc asp.net-core

我有一个网站,其中包含ali.sarahah.com等子域名,但如果用户从www.sarahah.com登录,则转到ali.sarahah.com会话未保存.搜索后我添加了以下内容Startup.cs:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    CookieDomain = ".sarahah.com"
});
Run Code Online (Sandbox Code Playgroud)

我发现.AspNetCore.Identity.Application cookie域仍然显示子域而不是域,并且该会话问题仍然存在.

难道我做错了什么?

Dav*_*ine 7

我认为您需要删除.域名分配中的前导,如此GitHub问题中所述:

app.UseCookieAuthentication(
    new CookieAuthenticationOptions
    {
        // Note that there is no leading .
        CookieDomain = "sarahah.com",
        CookieSecure = CookieSecurePolicy.None
    });
Run Code Online (Sandbox Code Playgroud)

请参阅CookieAuthenticationOptions各种属性.


Tec*_*chy 3

我可以通过将其添加到 Startup.cs 中的 ConfigureServices 方法来解决它:

        services.AddIdentity<ApplicationUser, IdentityRole>(options =>
        {
            options.Cookies.ApplicationCookie.CookieDomain = ".yourdomain.com";
            options.Cookies.ApplicationCookie.CookieSecure = Microsoft.AspNetCore.Http.CookieSecurePolicy.None;
        })
Run Code Online (Sandbox Code Playgroud)

CookieSecure 部分是因为我的网站在不同页面的 http 和 https 之间移动。

谢谢 :)