【发布时间】:2022-02-11 03:03:36
【问题描述】:
几天前,我的测试环境没有按预期工作,因为我的 cookie LanguageIdCookie 在我更改下拉列表中的语言时更新,停止工作,并且在更改任何语言时它的默认值为 1 .
我可以在 Edge 资源管理器中看到的控制台错误:
标有 SameSite=None 的 Cookie 也必须标有 Secure 以允许在跨站点上下文中设置它们。此行为可防止用户数据通过不安全的连接发送。
通过更新 cookie 的属性解决此问题:
如果要在跨站点上下文中设置 cookie,请指定 SameSite=None 和 Secure。请注意,只有通过 HTTPS 发送的 cookie 才能使用 Secure 属性。
如果跨站点请求不应设置 cookie,请指定 SameSite=Strict 或 SameSite=Lax。
我想强调的是,我的网站在 Firefox 中运行良好。
应用程序不会更新 Chrome 和 Edge 中的 cookie 值。
我认为我的代码/数据库一切正常(因为它在 firefox 中运行良好),所以你能告诉我我需要在我的 web.config 文件中做什么,或者在我的代码中的部分中放置一些东西吗我设置了我的 cookie:
public int CurrentLanguageId
{
get
{
if (HttpContext.Current != null &&
HttpContext.Current.Request != null &&
HttpContext.Current.Request.Cookies != null)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["LanguageIdCookie"];
if (cookie != null)
return int.Parse(cookie.Value);
}
return 1;
}
set
{
if (HttpContext.Current != null &&
HttpContext.Current.Request != null &&
HttpContext.Current.Response != null &&
HttpContext.Current.Request.Cookies != null)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["LanguageIdCookie"];
if (cookie == null)
{
cookie = new HttpCookie("LanguageIdCookie");
}
cookie.Value = value.ToString();
cookie.Expires = DateTime.UtcNow.AddDays(10);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
}
我试图把它放在我的 web.config 中,但我仍然没有更新 cookie 的值:
<system.web>
...
<httpCookies sameSite="Strict"/>
</system.web>
【问题讨论】:
标签: .net asp.net-mvc http cookies