@@ -56,6 +56,7 @@ public void Append(string key, string value, CookieOptions options)
5656 bool domainHasValue = ! string . IsNullOrEmpty ( options . Domain ) ;
5757 bool pathHasValue = ! string . IsNullOrEmpty ( options . Path ) ;
5858 bool expiresHasValue = options . Expires . HasValue ;
59+ bool sameSiteHasValue = options . SameSite . HasValue ;
5960
6061 string setCookieValue = string . Concat (
6162 Uri . EscapeDataString ( key ) ,
@@ -66,10 +67,12 @@ public void Append(string key, string value, CookieOptions options)
6667 ! pathHasValue ? null : "; path=" ,
6768 ! pathHasValue ? null : options . Path ,
6869 ! expiresHasValue ? null : "; expires=" ,
69- ! expiresHasValue ? null : options . Expires . Value . ToString ( "ddd, dd-MMM-yyyy HH:mm:ss " , CultureInfo . InvariantCulture ) + "GMT" ,
70+ ! expiresHasValue ? null : options . Expires . Value . ToString ( "ddd, dd-MMM-yyyy HH:mm:ss \\ G \\ M \\ T " , CultureInfo . InvariantCulture ) ,
7071 ! options . Secure ? null : "; secure" ,
71- ! options . HttpOnly ? null : "; HttpOnly" ) ;
72- Headers . AppendValues ( "Set-Cookie" , setCookieValue ) ;
72+ ! options . HttpOnly ? null : "; HttpOnly" ,
73+ ! sameSiteHasValue ? null : "; SameSite=" ,
74+ ! sameSiteHasValue ? null : GetStringRepresentationOfSameSite ( options . SameSite . Value ) ) ;
75+ Headers . AppendValues ( Constants . Headers . SetCookie , setCookieValue ) ;
7376 }
7477
7578 /// <summary>
@@ -138,5 +141,25 @@ public void Delete(string key, CookieOptions options)
138141 Expires = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ,
139142 } ) ;
140143 }
144+
145+ /// <summary>
146+ /// Analogous to ToString() but without boxing so
147+ /// we can save a bit of memory.
148+ /// </summary>
149+ private static string GetStringRepresentationOfSameSite ( SameSiteMode siteMode )
150+ {
151+ switch ( siteMode )
152+ {
153+ case SameSiteMode . None :
154+ return "None" ;
155+ case SameSiteMode . Lax :
156+ return "Lax" ;
157+ case SameSiteMode . Strict :
158+ return "Strict" ;
159+ default :
160+ throw new ArgumentOutOfRangeException ( "siteMode" ,
161+ string . Format ( CultureInfo . InvariantCulture , "Unexpected SameSiteMode value: {0}" , siteMode ) ) ;
162+ }
163+ }
141164 }
142165}
0 commit comments