【问题标题】:How to set vaue of attribute samesite on the cookie __RequestVerificationToken_Lw__如何在 cookie 上设置属性 samesite 的值 __RequestVerificationToken ___
【发布时间】:2019-10-31 06:02:56
【问题描述】:

我在一个 cshtml 页面上有一个防伪造令牌 (@Html.AntiForgeryToken()),它会生成一个 cookie RequestVerificationToken_Lw。此 cookie 的属性值为 HTTP 和 Secure。但我也需要设置 SameSite。我如何做到这一点?

@Html.AntiForgeryToken()

__RequestVerificationToken_Lw__

【问题讨论】:

  • 疯狂猜测.. 在您的启动课程中设置它? services.AddAntiforgery(options => { options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict ; });

标签: html token antiforgerytoken samesite


【解决方案1】:

这有帮助吗?

在 Global.asax.cs 中

 public class MvcApplication : System.Web.HttpApplication
 {

        protected void Application_PreSendRequestHeaders(object sender,EventArgs e) {
            // This code will mark the __RequestVerificationToken cookie SameSite=Strict 
            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.Strict;
                        Response.Cookies.Set(c);
                    }
                }
            }           
        }
 }

【讨论】:

  • SameSite 在 .NET Framework 4.7.2 之前不可用 :-(
  • @PaulB.,在早期的 .Net 中,您可以使用 c.Path += "; SameSite=Strict";。 (注意:您可能需要先检查现有的SameSite-part。)
  • 这看起来实际上是第二次添加 cookie,而不是覆盖 cookie
猜你喜欢
  • 2020-07-04
  • 1970-01-01
  • 2018-11-21
  • 2020-04-25
  • 2019-06-03
  • 2019-05-14
  • 2020-04-18
  • 2021-01-04
  • 2018-11-13
相关资源
最近更新 更多