17
17
// </copyright>
18
18
19
19
using System ;
20
+ using System . Linq ;
20
21
using System . Collections . Generic ;
21
22
using System . Globalization ;
22
23
using Newtonsoft . Json ;
@@ -35,8 +36,11 @@ public class Cookie
35
36
private string cookieValue ;
36
37
private string cookiePath ;
37
38
private string cookieDomain ;
39
+ private bool isHttpOnly ;
38
40
private string sameSite ;
41
+ private bool secure ;
39
42
private DateTime ? cookieExpiry ;
43
+ private readonly string [ ] sameSiteValues = { "Strict" , "Lax" , "None" } ;
40
44
41
45
/// <summary>
42
46
/// 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)
98
102
{
99
103
}
100
104
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
+
101
143
/// <summary>
102
144
/// Initializes a new instance of the <see cref="Cookie"/> class with a specific name,
103
145
/// value, and path.
@@ -168,7 +210,7 @@ public virtual string Path
168
210
[ JsonProperty ( "secure" ) ]
169
211
public virtual bool Secure
170
212
{
171
- get { return false ; }
213
+ get { return this . secure ; }
172
214
}
173
215
174
216
/// <summary>
@@ -177,7 +219,8 @@ public virtual bool Secure
177
219
[ JsonProperty ( "httpOnly" ) ]
178
220
public virtual bool IsHttpOnly
179
221
{
180
- get { return false ; }
222
+ get { return this . isHttpOnly ; }
223
+
181
224
}
182
225
183
226
/// <summary>
@@ -187,7 +230,6 @@ public virtual bool IsHttpOnly
187
230
public virtual string SameSite
188
231
{
189
232
get { return this . sameSite ; }
190
- protected set { this . sameSite = value ; }
191
233
}
192
234
193
235
/// <summary>
@@ -287,7 +329,8 @@ public override string ToString()
287
329
return this . cookieName + "=" + this . cookieValue
288
330
+ ( this . cookieExpiry == null ? string . Empty : "; expires=" + this . cookieExpiry . Value . ToUniversalTime ( ) . ToString ( "ddd MM dd yyyy hh:mm:ss UTC" , CultureInfo . InvariantCulture ) )
289
331
+ ( 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 ) ;
291
334
}
292
335
293
336
/// <summary>
0 commit comments