1717// </copyright>
1818
1919using System ;
20+ using System . Linq ;
2021using System . Collections . Generic ;
2122using System . Globalization ;
2223using Newtonsoft . Json ;
@@ -35,8 +36,11 @@ public class Cookie
3536 private string cookieValue ;
3637 private string cookiePath ;
3738 private string cookieDomain ;
39+ private bool isHttpOnly ;
3840 private string sameSite ;
41+ private bool secure ;
3942 private DateTime ? cookieExpiry ;
43+ private readonly string [ ] sameSiteValues = { "Strict" , "Lax" , "None" } ;
4044
4145 /// <summary>
4246 /// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
@@ -98,6 +102,44 @@ public Cookie(string name, string value, string path, DateTime? expiry)
98102 {
99103 }
100104
105+ /// <summary>
106+ /// Initializes a new instance of the <see cref="ReturnedCookie"/> class with a specific name,
107+ /// value, domain, path and expiration date.
108+ /// </summary>
109+ /// <param name="name">The name of the cookie.</param>
110+ /// <param name="value">The value of the cookie.</param>
111+ /// <param name="domain">The domain of the cookie.</param>
112+ /// <param name="path">The path of the cookie.</param>
113+ /// <param name="expiry">The expiration date of the cookie.</param>
114+ /// <param name="isSecure"><see langword="true"/> if the cookie is secure; otherwise <see langword="false"/></param>
115+ /// <param name="isHttpOnly"><see langword="true"/> if the cookie is an HTTP-only cookie; otherwise <see langword="false"/></param>
116+ /// <param name="sameSite">The SameSite value of cookie.</param>
117+ /// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
118+ /// or if it contains a semi-colon.</exception>
119+ /// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
120+ /// <exception cref="ArgumentNullException">If the same site value is not valid or same site value is "None" but secure is set to false.</exception>
121+ public Cookie ( string name , string value , string domain , string path , DateTime ? expiry , bool secure , bool isHttpOnly , string sameSite )
122+ : this ( name , value , domain , path , expiry )
123+ {
124+ this . isHttpOnly = isHttpOnly ;
125+ this . secure = secure ;
126+
127+ if ( ! string . IsNullOrEmpty ( sameSite ) )
128+ {
129+ if ( ! sameSiteValues . Contains ( sameSite ) )
130+ {
131+ throw new ArgumentException ( "Invalid sameSite cookie value. It should either \" Lax\" , \" Strict\" or \" None\" " , "sameSite" ) ;
132+ }
133+
134+ if ( "None" . Equals ( sameSite ) && ! this . secure )
135+ {
136+ throw new ArgumentException ( "Invalid cookie configuration: SameSite=None must be Secure" ) ;
137+ }
138+
139+ this . sameSite = sameSite ;
140+ }
141+ }
142+
101143 /// <summary>
102144 /// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
103145 /// value, and path.
@@ -168,7 +210,7 @@ public virtual string Path
168210 [ JsonProperty ( "secure" ) ]
169211 public virtual bool Secure
170212 {
171- get { return false ; }
213+ get { return this . secure ; }
172214 }
173215
174216 /// <summary>
@@ -177,7 +219,8 @@ public virtual bool Secure
177219 [ JsonProperty ( "httpOnly" ) ]
178220 public virtual bool IsHttpOnly
179221 {
180- get { return false ; }
222+ get { return this . isHttpOnly ; }
223+
181224 }
182225
183226 /// <summary>
@@ -187,7 +230,6 @@ public virtual bool IsHttpOnly
187230 public virtual string SameSite
188231 {
189232 get { return this . sameSite ; }
190- protected set { this . sameSite = value ; }
191233 }
192234
193235 /// <summary>
@@ -287,7 +329,8 @@ public override string ToString()
287329 return this . cookieName + "=" + this . cookieValue
288330 + ( this . cookieExpiry == null ? string . Empty : "; expires=" + this . cookieExpiry . Value . ToUniversalTime ( ) . ToString ( "ddd MM dd yyyy hh:mm:ss UTC" , CultureInfo . InvariantCulture ) )
289331 + ( string . IsNullOrEmpty ( this . cookiePath ) ? string . Empty : "; path=" + this . cookiePath )
290- + ( string . IsNullOrEmpty ( this . cookieDomain ) ? string . Empty : "; domain=" + this . cookieDomain ) ;
332+ + ( string . IsNullOrEmpty ( this . cookieDomain ) ? string . Empty : "; domain=" + this . cookieDomain )
333+ + "; isHttpOnly= " + this . isHttpOnly + "; secure= " + this . secure + ( string . IsNullOrEmpty ( this . sameSite ) ? string . Empty : "; sameSite=" + this . sameSite ) ;
291334 }
292335
293336 /// <summary>
0 commit comments