how SameSite attribute added to my Asp.net_SessionID cookie automatically?

前端 未结 7 1374
迷失自我
迷失自我 2020-11-30 06:47

Recently samesite=lax add automatically to my session cookie! this attribute just add to sessionID: \"Set-Cookie ASP.NET_SessionId=zana3mklplqwewhwvika2125; path=/; H

相关标签:
7条回答
  • 2020-11-30 07:20

    CookieSameSite attribute is not available for many older frameworks. If you're in the situation where the accepted answer is not supported in your environment, read on!

    I modified upon several SO answers to come up with this URL rewrite that adds SameSite=None to session cookies, and also remove SameSite=None from all cookies for most incompatible browsers. The aim of this rewrite is to preserve the "legacy" behaviour pre-Chrome 80.

    Full write-up in my Coder Frontline blog:

    <rewrite>
      <outboundRules>
        <preConditions>
          <!-- Checks User Agent to identify browsers incompatible with SameSite=None -->
          <preCondition name="IncompatibleWithSameSiteNone" logicalGrouping="MatchAny">
            <add input="{HTTP_USER_AGENT}" pattern="(CPU iPhone OS 12)|(iPad; CPU OS 12)" />
            <add input="{HTTP_USER_AGENT}" pattern="(Chrome/5)|(Chrome/6)" />
            <add input="{HTTP_USER_AGENT}" pattern="( OS X 10_14).*(Version/).*((Safari)|(KHTML, like Gecko)$)" />
          </preCondition>
        </preConditions>
    
        <!-- Adds or changes SameSite to None for the session cookie -->
        <!-- Note that secure header is also required by Chrome and should not be added here -->
        <rule name="SessionCookieAddNoneHeader">
          <match serverVariable="RESPONSE_Set-Cookie" pattern="(.*ASP.NET_SessionId.*)" />
          <!-- Use this regex if your OS/framework/app adds SameSite=Lax automatically to the end of the cookie -->
          <!-- <match serverVariable="RESPONSE_Set-Cookie" pattern="((.*)(ASP.NET_SessionId)(=.*))(?=SameSite)" /> -->
          <action type="Rewrite" value="{R:1}; SameSite=None" />
        </rule>
    
        <!-- Removes SameSite=None header from all cookies, for most incompatible browsers -->
        <rule name="CookieRemoveSameSiteNone" preCondition="IncompatibleWithSameSiteNone">
          <match serverVariable="RESPONSE_Set-Cookie" pattern="(.*)(SameSite=None)" />
          <action type="Rewrite" value="{R:1}" />
        </rule>
      </outboundRules>
    </rewrite>
    

    This should work for most ASP .Net and ASP .Net Core applications, although newer Frameworks have proper code and config options to let you control this behaviour. I would recommend researching all the options available to you before using my rewrite above.

    0 讨论(0)
提交回复
热议问题