.NET Core SameSite cookie问题

  • A+
所属分类:.NET技术
摘要

This setcookie was blocked because it had the "samesite=none" attribute but did not have the "secure" attribute, which is required in order to use "same=none"


异常提示

This setcookie was blocked because it had the "samesite=none" attribute but did not have the "secure" attribute, which is required in order to use "same=none"

SameSite

SameSite 有3个可选值 :

  • Strict 禁止第三方 cookie
  • Lax
  • None 关闭 SameSite 选项.

其实在谷歌升级后呢,默认限制了跨域携带cookie给后端,在使用samesite=none后其实不仅仅如此,我们还需要设置Secure属性才可以生效,但是对于项目中有些不可告人的秘密,哈哈哈哈哈,所以此处不从项目中修改了我们将修Nginx服务器来解决SameSite的问题。

Nginx proxy_cookie_path

具体配置方法(在location节点下加入,配置后重载Nginx):

  • 站点Cookie所在目录在根目录/下,设置如下:
    proxy_cookie_path / “/; secure; SameSite=None”;
  • 站点Cookie所在目录在abc目录下,设置如下:
    proxy_cookie_path /abc/ “/abc/; secure; SameSite=None”;
  • 如果无法确定站点Cookie目录,可使用Chrome开发者工具,监测Network下网络请求,找到Response Headers中set-cookie属性值,该值中有path属性值即为Cookie目录,也即上文要替换的/或者/abc/值。

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cookie_path
https://docs.microsoft.com/zh-cn/aspnet/samesite/system-web-samesite