loading

Check for support

IsSupported

CookieStore is not available in every browser, so feature-detect before relying on it. When it is missing, fall back to the legacy Cookie service - it works everywhere but only exposes name/value pairs.

C#
@inject Bit.Butil.CookieStore cookieStore

if (await cookieStore.IsSupported())
{
    // safe to use cookieStore
}
Live sample
support output
Results will appear here when you interact with the samples.

Read with full metadata

Get / GetAll

This is where CookieStore earns its keep: Get and GetAll return CookieStoreItem objects with every attribute the browser knows - domain, path, expiry, secure flag, SameSite policy and partitioned state - not just the name and value.

C#
var one = await cookieStore.Get("butil-docs");   // CookieStoreItem? - null when absent

var all = await cookieStore.GetAll();            // CookieStoreItem[]

foreach (var c in all)
{
    // c.Name, c.Value, c.Domain, c.Path, c.Expires, c.Secure, c.SameSite, c.Partitioned
}
Live sample
Name
read output
Results will appear here when you interact with the samples.

Watch cookies for changes

SubscribeChange

The change event is the one thing document.cookie can never give you. It fires for cookies written by script, by a server's Set-Cookie header, and on expiry - which makes it the way to notice a session cookie dying without polling for it. Each notification carries both halves: cookies written and cookies removed. An overwrite arrives as a delete plus a set, so both lists can be non-empty at once.

C#
private ButilSubscription? subscription;

subscription = await cookieStore.SubscribeChange(change =>
{
    foreach (var c in change.Changed) { /* written or updated */ }
    foreach (var c in change.Deleted) { /* removed or expired */ }
});

if (subscription is null)
{
    // no CookieStore here - document.cookie has no change event to fall back on
}

// later
await subscription.DisposeAsync();
Live sample
Cookie to write and remove
change output
Results will appear here when you interact with the samples.
Warning:
Browser support: Chromium-only (for now) CookieStore is currently implemented in Chromium-based browsers (Chrome, Edge, Opera) and requires a secure context (HTTPS or localhost). Firefox and Safari have not shipped it at the time of writing - always gate usage behind IsSupported and fall back to the Cookie service elsewhere.
Note:
Legacy Cookie vs CookieStore Both services write the same cookies. Choose Cookie for universal support with name/value access, and CookieStore when you need to read attributes back or prefer a promise-based API. HttpOnly cookies remain invisible to both - that protection applies to all script access.

API reference

Member
Signature
Description
IsSupported
ValueTask<bool> IsSupported()
True when the runtime exposes cookieStore.
GetAll
ValueTask<CookieStoreItem[]> GetAll()
Returns every cookie visible to the current document, with full metadata.
Get
ValueTask<CookieStoreItem?> Get(string name)
Returns the cookie with the given name, or null when absent.
Set
ValueTask Set(CookieStoreItem cookie)
Sets a cookie from the item's populated attributes.
Delete
ValueTask Delete(string name)
Deletes the named cookie.
SubscribeChange
ValueTask<ButilSubscription?> SubscribeChange(Action<CookieStoreChange> handler)
Calls the handler whenever cookies are written or removed - by script, by a Set-Cookie header, or on expiry. Null where CookieStore is unsupported.
CookieStoreItem
class CookieStoreItem { string Name; string Value; string? Domain; string? Path; DateTimeOffset? Expires; bool Secure; string? SameSite; bool? Partitioned; }
Cookie model with the complete attribute set. Expires is null for session cookies; SameSite is one of strict, lax, none, or null.
CookieStoreChange
class CookieStoreChange { CookieStoreItem[] Changed; CookieStoreItem[] Deleted; }
One batch of changes. Both lists can be non-empty at once, since an overwrite is reported as a delete followed by a set.
An unhandled error has occurred. Reload 🗙