【问题标题】:How can I set SameSite=None on the AntiForgertyToken cookie in MVC5?如何在 MVC5 中的 AntiForgertyToken cookie 上设置 SameSite=None?
【发布时间】:2021-03-10 03:37:02
【问题描述】:

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

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

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

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

如何在 cookie 上获取此设置?

【问题讨论】:

    标签: asp.net-mvc-5 antiforgerytoken


    【解决方案1】:

    将此添加到 global.asax.cs 以将令牌设置为 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);
                        }
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 2020-05-17
      • 2019-12-18
      • 2020-05-22
      • 2021-01-09
      • 2020-05-01
      相关资源
      最近更新 更多