CookieJar Class

A cookie store as specified by the user agent requirements of RFC 6265bis (https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html#name-user-agent-requirements): the cookies of the Set-Cookie headers of the responses and of the scripts (document.cookie, cookieStore), sent in the Cookie header of the requests (see DocumentRequest.GetCookieHeader).

public sealed class CookieJar

Remarks

The jar implements the storage model (the Domain, Path, Expires, Max-Age, Secure, HttpOnly, SameSite and Partitioned attributes, the __Secure-, __Host-, __Http- and __Host-Http- prefixes, the public suffixes and the size limits) and the retrieval model (the "same-site" and "cross-site" requests, schemefully). The cookies with the Partitioned attribute are keyed by the site of the top-level document (CHIPS); the other cookies are not partitioned, and the third-party cookies are not blocked.

The names and the values of the cookies are byte sequences, represented as strings whose characters are the bytes (isomorphic strings, as the values of the HTTP headers): a script stores the UTF-8 bytes of its strings. The expiry times are measured by the clock given to the constructor (e.g. BrowsingContextOptions.TimeProvider). The jar is thread-safe: it can be shared by several browsing contexts.

The cookies are kept in memory, and the persistent cookies (the cookies with an expiry time) are persisted by the CookiePersistence given to the constructor, if any.

Constructors

NameDescription
CookieJar()Creates an empty cookie jar, whose expiry times are measured by the system clock.
CookieJar(CookiePersistence?, TimeProvider?)Creates a cookie jar whose persistent cookies are persisted: the jar contains the cookies of CookiePersistence.LoadCookies that are not expired, and reports its changes to the persistence.
CookieJar(TimeProvider?)Creates an empty cookie jar.

CookieJar overloads

CookieJar()

CookieJar.cs

Creates an empty cookie jar, whose expiry times are measured by the system clock.

public CookieJar()

CookieJar(CookiePersistence?, TimeProvider?)

CookieJar.cs

Creates a cookie jar whose persistent cookies are persisted: the jar contains the cookies of CookiePersistence.LoadCookies that are not expired, and reports its changes to the persistence.

public CookieJar(CookiePersistence? persistence, TimeProvider? timeProvider)
Parameters
persistence CookiePersistence?
The persistent storage of the cookies, or null to keep them in memory only.
timeProvider TimeProvider?
The clock that measures the creation and expiry times of the cookies, or null for TimeProvider.System.

CookieJar(TimeProvider?)

CookieJar.cs

Creates an empty cookie jar.

public CookieJar(TimeProvider? timeProvider)
Parameters
timeProvider TimeProvider?
The clock that measures the creation and expiry times of the cookies, or null for TimeProvider.System.

Properties

NameDescription
PersistenceGets the persistent storage of the cookies, or null when they are kept in memory only.

Persistence

CookieJar.cs

Gets the persistent storage of the cookies, or null when they are kept in memory only.

public CookiePersistence? Persistence { get; }

Property value

CookiePersistence?

Methods

NameDescription
Clear()Removes all the cookies, and the persisted cookies of CookieJar.Persistence.
GetCookies()Returns the cookies of the jar that are not expired, in the order of their creation.
GetCookies(string)Returns the cookies that would be sent to url by a request that is same-site with it, including the HttpOnly cookies (the associated cookies of WebDriver, https://w3c.github.io/webdriver/#dfn-associated-cookies), in the order of the Cookie header. The partitioned cookies are the ones of the site of the URL.
Remove(StoredCookie)Removes a cookie (with the same name, domain, path and partition). Returns false when the jar does not contain it.
SetCookie(string, string)Stores the cookie of a Set-Cookie header received from url in a response to a same-site request (e.g. to restore cookies). Returns false when the cookie is ignored (an invalid cookie, or a cookie that the URL cannot set).

Removes all the cookies, and the persisted cookies of CookieJar.Persistence.

public void Clear()

GetCookies overloads

GetCookies()

CookieJar.cs

Returns the cookies of the jar that are not expired, in the order of their creation.

public IReadOnlyList<StoredCookie> GetCookies()

GetCookies(string)

CookieJar.cs

Returns the cookies that would be sent to url by a request that is same-site with it, including the HttpOnly cookies (the associated cookies of WebDriver, https://w3c.github.io/webdriver/#dfn-associated-cookies), in the order of the Cookie header. The partitioned cookies are the ones of the site of the URL.

public IReadOnlyList<StoredCookie> GetCookies(string url)
Parameters
url string
An absolute URL.
Returns

IReadOnlyList<StoredCookie>

Remove(StoredCookie)

CookieJar.cs

Removes a cookie (with the same name, domain, path and partition). Returns false when the jar does not contain it.

public bool Remove(StoredCookie cookie)

Parameters

cookie StoredCookie

Returns

bool

SetCookie(string, string)

CookieJar.cs

Stores the cookie of a Set-Cookie header received from url in a response to a same-site request (e.g. to restore cookies). Returns false when the cookie is ignored (an invalid cookie, or a cookie that the URL cannot set).

public bool SetCookie(string url, string setCookieHeader)

Parameters

url string
The absolute URL of the response.
setCookieHeader string
The value of the Set-Cookie header (an isomorphic string: each character is a byte).

Returns

bool