我在网上冲浪,发现了一篇文章使用同站cookie属性防止CSRF.
在链接维护时,我们需要添加 Set-Cookie 标头。
设置-Cookie:键=值;仅 Http; SameSite=严格
现在我的问题是,我想在我的 ASP.NET 站点中的所有 Cookie 和身份验证 Cookie 中进行设置。
我尝试使用 IIS 的标头来设置此值,但有人说这是错误的实现方式。
我也在下面尝试过。
HttpCookie newAuthenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName
, FormsAuthentication.Encrypt(newAuthenticationTicket))
{
HttpOnly = true
};
newAuthenticationCookie.Values.Add("SameSite", "strict");
但似乎对我没有帮助。
请建议我一个更好的方法来做到这一点。
Thanks.
深入审查后HttpCookie 源确认我们无法使用代码执行此操作,因为无法在 Cookie 上添加额外的属性,并且类被标记为密封。
但无论如何我仍然通过修改来管理解决方案网络配置如下。
<rewrite>
<outboundRules>
<rule name="Add SameSite" preCondition="No SameSite">
<match serverVariable="RESPONSE_Set_Cookie" pattern=".*" negate="false" />
<action type="Rewrite" value="{R:0}; SameSite=strict" />
<conditions>
</conditions>
</rule>
<preConditions>
<preCondition name="No SameSite">
<add input="{RESPONSE_Set_Cookie}" pattern="." />
<add input="{RESPONSE_Set_Cookie}" pattern="; SameSite=strict" negate="true" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
这个添加SameSite=严格在各个设置Cookie.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)