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 CookieJarRemarks
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
| Name | Description |
|---|---|
| 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.csCreates an empty cookie jar, whose expiry times are measured by the system clock.
public CookieJar()CookieJar(CookiePersistence?, TimeProvider?)
CookieJar.csCreates 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
persistenceCookiePersistence?- The persistent storage of the cookies, or null to keep them in memory only.
timeProviderTimeProvider?- The clock that measures the creation and expiry times of the cookies, or null for
TimeProvider.System.
CookieJar(TimeProvider?)
CookieJar.csCreates an empty cookie jar.
public CookieJar(TimeProvider? timeProvider)Parameters
timeProviderTimeProvider?- The clock that measures the creation and expiry times of the cookies, or null for
TimeProvider.System.
Properties
| Name | Description |
|---|---|
| Persistence | Gets the persistent storage of the cookies, or null when they are kept in memory only. |
Persistence
CookieJar.csGets the persistent storage of the cookies, or null when they are kept in memory only.
public CookiePersistence? Persistence { get; }Property value
Methods
| Name | Description |
|---|---|
| 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). |
Clear()
CookieJar.csRemoves all the cookies, and the persisted cookies of CookieJar.Persistence.
public void Clear()GetCookies overloads
GetCookies()
CookieJar.csReturns the cookies of the jar that are not expired, in the order of their creation.
public IReadOnlyList<StoredCookie> GetCookies()Returns
GetCookies(string)
CookieJar.csReturns 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)Remove(StoredCookie)
CookieJar.csRemoves a cookie (with the same name, domain, path and partition). Returns false when the jar does not contain it.
public bool Remove(StoredCookie cookie)SetCookie(string, string)
CookieJar.csStores 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).