Cookie不会在外部网站上持续存在

时间:2018-10-03 13:40:40

标签: javascript google-chrome session asp.net-core google-chrome-extension

当我从Google扩展程序通过GitHub.com调用服务器时,Asp.net核心会话始终为空。

如果我通过自己的localhost:8080网站进行完全相同的操作,则它可以正常工作,但似乎来自外部域的请求不会保留该会话的cookie。

Startup.cs

services.AddSession(options => new SessionOptions
{
    Cookie = new CookieBuilder
    {
        SameSite = SameSiteMode.None,
        IsEssential = true,
        Name = ".onboardor",
    }
});

app.UseSession(new SessionOptions
{
    Cookie = new CookieBuilder
    {
        SameSite = SameSiteMode.None,
        IsEssential = true,
        Name = ".onboardor",
    }
});

不知道为什么它们是传递会话选项的两种方法...

public IActionResult Test()
{
    var value = HttpContext.Session.GetString("Test");

    HttpContext.Session.SetString("Test", "randomValue");

    return null;
}

js:

fetch("https://localhost:8080/test", {
  credentials: "include",
  headers: {
    "Accept": "application/json",
    "Content-Type": "application/json",
  },
  method: "get",
});

enter image description here

您可以在上图中看到cookie确实存在,但是刷新页面时似乎会立即删除它。

1 个答案:

答案 0 :(得分:0)

弄清楚了。会话无法正常运行,因为对于CORS,您需要将Cookie的相同站点模式设置为无。

        app.UseCookiePolicy(new CookiePolicyOptions
        {
            MinimumSameSitePolicy = SameSiteMode.None
        });

        app.UseSession(new SessionOptions {
            Cookie = new CookieBuilder
            {
                Name = ".AspNetCore.Session",
                SameSite = SameSiteMode.None,
            }
        });