簡體   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