Skip to content

Commit c543c22

Browse files
committed
Adding SameSite cookie information to .NET cookie handling
1 parent 13d830b commit c543c22

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

dotnet/src/webdriver/Cookie.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Cookie
3535
private string cookieValue;
3636
private string cookiePath;
3737
private string cookieDomain;
38+
private string sameSite;
3839
private DateTime? cookieExpiry;
3940

4041
/// <summary>
@@ -179,6 +180,16 @@ public virtual bool IsHttpOnly
179180
get { return false; }
180181
}
181182

183+
/// <summary>
184+
/// Gets the SameSite setting for the cookie.
185+
/// </summary>
186+
[JsonProperty("sameSite")]
187+
public virtual string SameSite
188+
{
189+
get { return this.sameSite; }
190+
protected set { this.sameSite = value; }
191+
}
192+
182193
/// <summary>
183194
/// Gets the expiration date of the cookie.
184195
/// </summary>
@@ -258,7 +269,13 @@ public static Cookie FromDictionary(Dictionary<string, object> rawCookie)
258269
isHttpOnly = bool.Parse(rawCookie["httpOnly"].ToString());
259270
}
260271

261-
return new ReturnedCookie(name, value, domain, path, expires, secure, isHttpOnly);
272+
string sameSite = null;
273+
if (rawCookie.ContainsKey("sameSite") && rawCookie["sameSite"] != null)
274+
{
275+
sameSite = rawCookie["sameSite"].ToString();
276+
}
277+
278+
return new ReturnedCookie(name, value, domain, path, expires, secure, isHttpOnly, sameSite);
262279
}
263280

264281
/// <summary>

dotnet/src/webdriver/Internal/ReturnedCookie.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="ReturnedCookie.cs" company="WebDriver Committers">
1+
// <copyright file="ReturnedCookie.cs" company="WebDriver Committers">
22
// Licensed to the Software Freedom Conservancy (SFC) under one
33
// or more contributor license agreements. See the NOTICE file
44
// distributed with this work for additional information
@@ -44,10 +44,31 @@ public class ReturnedCookie : Cookie
4444
/// or if it contains a semi-colon.</exception>
4545
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
4646
public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly)
47+
: this(name, value, domain, path, expiry, isSecure, isHttpOnly, null)
48+
{
49+
}
50+
51+
/// <summary>
52+
/// Initializes a new instance of the <see cref="ReturnedCookie"/> class with a specific name,
53+
/// value, domain, path and expiration date.
54+
/// </summary>
55+
/// <param name="name">The name of the cookie.</param>
56+
/// <param name="value">The value of the cookie.</param>
57+
/// <param name="domain">The domain of the cookie.</param>
58+
/// <param name="path">The path of the cookie.</param>
59+
/// <param name="expiry">The expiration date of the cookie.</param>
60+
/// <param name="isSecure"><see langword="true"/> if the cookie is secure; otherwise <see langword="false"/></param>
61+
/// <param name="isHttpOnly"><see langword="true"/> if the cookie is an HTTP-only cookie; otherwise <see langword="false"/></param>
62+
/// <param name="sameSite">The SameSite value of cookie.</param>
63+
/// <exception cref="ArgumentException">If the name is <see langword="null"/> or an empty string,
64+
/// or if it contains a semi-colon.</exception>
65+
/// <exception cref="ArgumentNullException">If the value or currentUrl is <see langword="null"/>.</exception>
66+
public ReturnedCookie(string name, string value, string domain, string path, DateTime? expiry, bool isSecure, bool isHttpOnly, string sameSite)
4767
: base(name, value, domain, path, expiry)
4868
{
4969
this.isSecure = isSecure;
5070
this.isHttpOnly = isHttpOnly;
71+
this.SameSite = sameSite;
5172
}
5273

5374
/// <summary>

dotnet/test/common/CookieImplementationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public void CanHandleSecureCookie()
474474
{
475475
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIsSecure("animals");
476476

477-
Cookie addedCookie = new ReturnedCookie("fish", "cod", null, "/common/animals", null, true, false);
477+
Cookie addedCookie = new ReturnedCookie("fish", "cod", null, "/common/animals", null, true, false, null);
478478
driver.Manage().Cookies.AddCookie(addedCookie);
479479

480480
driver.Navigate().Refresh();
@@ -490,7 +490,7 @@ public void ShouldRetainCookieSecure()
490490
{
491491
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIsSecure("animals");
492492

493-
ReturnedCookie addedCookie = new ReturnedCookie("fish", "cod", string.Empty, "/common/animals", null, true, false);
493+
ReturnedCookie addedCookie = new ReturnedCookie("fish", "cod", string.Empty, "/common/animals", null, true, false, null);
494494

495495
driver.Manage().Cookies.AddCookie(addedCookie);
496496

0 commit comments

Comments
 (0)