繁体   English   中英

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

[英]How can I share session among subdomains in ASP.NET Core?

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

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    CookieDomain = ".sarahah.com"
});

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

难道我做错了什么?

我认为你需要删除领先. 在此GitHub问题中详细说明的域分配中:

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

有关各种属性,请参阅CookieAuthenticationOptions

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

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

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

谢谢 :)

如果有人正在使用ASP.NET Core 2.0寻找此问题的解决方案。 添加身份验证服务时,可以通过ConfigureServices方法中的CookieAuthenticationOptions设置cookie域。

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options =>
        {
            options.Cookie.Domain = ".yourdomain.com";
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM