Cookies上经典的ASP使用“ SameSite”

问题描述

我们正在使用Classic ASP通过Response.Cookies(“ CookieName ”)构造Cookie。我们将如何将“ SameSite”设置为无?

解决方法

尝试此操作(您需要安装URLRewrite模块)。您还需要使用https协议(SameSite仅在还包含Secure的情况下有效,并且如果不使用https协议就不能包含Secure)。 HttpOnly也应始终使用,但是如果您的站点上有一些需要读取Cookie的JavaScript代码,则HttpOnly可以防止这种情况。

您可能还需要在URLRewrite下的IIS中的“允许的服务器变量”中添加“ HTTP_COOKIE”。但是我认为那只是为了读取传入的Cookie。

编辑:经过尝试和测试,完美运行。

注意:如果您已经在使用Response.Cookies("CookieName").Secure = True,它将两次将Secure添加到响应标头值中(除非您从动作重写值中删除Secure),两次应该不会有什么问题,但是某些浏览器可能会对此类事情大惊小怪,尤其是Chrome浏览器,因为Google继续通过严格的Cookie规则来发挥越来越多的更新作用。

httpProtocol > customHeaders部分是完全可选的,但是它将为您的网站增加更多的安全性。

web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
        <outboundRules>
            <rule name="SameSite rewrite">
                <match serverVariable="RESPONSE_Set_Cookie" pattern="(.*)=(.*)" negate="false" />
                <action type="Rewrite" value="{R:1}={R:2}; SameSite=None; HttpOnly; Secure" />
            </rule>     
        </outboundRules>
    </rewrite>
    <httpProtocol>
      <customHeaders>
        <add name="X-Frame-Options" value="SAMEORIGIN" />
        <add name="X-Content-Type-Options" value="nosniff" />
        <add name="X-XSS-Protection" value="1; mode=block" />
        <add name="Referrer-Policy" value="strict-origin" />
        <add name="Strict-Transport-Security" value="max-age=31536000" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>