我在网上冲浪并发现文章Preventing CSRF with the same-site cookie attribute .
在链接维护时,我们需要添加 Set-Cookie header 。
Set-Cookie: key=value; HttpOnly; SameSite=strict
现在我的问题是,我想在我的 ASP.NET 站点中的所有 Cookie 和身份验证 Cookie 中进行设置。 我尝试使用 IIS 的 header 来设置此值,但有人说这是错误的实现方式。
我也尝试过以下。
HttpCookie newAuthenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName
, FormsAuthentication.Encrypt(newAuthenticationTicket))
{
HttpOnly = true
};
newAuthenticationCookie.Values.Add("SameSite", "strict");
但这似乎对我没有帮助。
请建议我一个更好的方法来做到这一点。
谢谢。
最佳答案
对 HttpCookie Source 进行深度审核后确认我们无法使用代码执行此操作,因为无法在 Cookie 上添加额外的属性,并且类被标记为密封。
But still anyhow I manage solution by modifying web.config as below.
<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>
这会在每个 Set-Cookie 上添加 SameSite=strict。
关于asp.net - 使用同站cookie属性防止CSRF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954821/