gpt4 book ai didi

asp.net-mvc-5 - 如何在 MVC5 中的 AntiForgertyToken cookie 上设置 SameSite=None?

转载 作者:行者123 更新时间:2023-12-05 04:52:20 30 4
gpt4 key购买 nike

我们通过使用内置的 ValidateAntiForgeryToken 属性和 @Html.AntiForgeryToken() 帮助程序在 MVC5 中实现跨站点脚本保护。

这一切都有效。但是,我们的应用程序在不同域的框架中运行。因此,我们需要将 cookie 设置为 SameSite=none(就像我们对 session 和身份验证 cookie 所做的那样)。

我找不到配置 cookie 以包含此设置的方法。

我试图创建一个 OWIN 中间件来检查输出的 cookie 并更新它,但是 OWIN 上下文中响应中的 cookie 集合是只读的。

如何在 cookie 上获得此设置?

最佳答案

将此添加到 global.asax.cs 以将 token 设置为 Same Site = none 应该可以修复它

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
// This code will mark the __RequestVerificationToken cookie SameSite=None
if (Request.Cookies.Count > 0)
{
foreach (string s in Request.Cookies.AllKeys)
{
if (s.ToLower() == "__requestverificationtoken")
{
HttpCookie c = Request.Cookies[s];
c.SameSite = System.Web.SameSiteMode.None;
Response.Cookies.Set(c);
}
}
}
}

关于asp.net-mvc-5 - 如何在 MVC5 中的 AntiForgertyToken cookie 上设置 SameSite=None?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66551481/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com