loading

Set with attributes

Set + ButilCookie options

ButilCookie carries every attribute the Set-Cookie syntax supports: Expires, MaxAge, Path, Domain, SameSite, Secure and Partitioned. Expires is emitted in RFC 1123 format; when both Expires and MaxAge are present the browser honors MaxAge.

C#
await cookie.Set(new ButilCookie
{
    Name = "session-hint",
    Value = "abc123",
    MaxAge = 3600,               // seconds
    Path = "/",
    SameSite = SameSite.Strict,
    Secure = true,
});

// or with an absolute expiry:
await cookie.Set(new ButilCookie
{
    Name = "campaign",
    Value = "spring",
    Expires = DateTimeOffset.UtcNow.AddDays(7),
});
Live sample
Name
Value
Max-Age (seconds)
Path
SameSite
attributes output
Results will appear here when you interact with the samples.

Read cookies

Get / GetValue / GetAll

Get returns a single ButilCookie by name, GetValue shortcuts straight to its string value, and GetAll parses every cookie visible to the document. The browser only exposes name=value pairs on read, so attributes such as Path or Expires are never populated on returned cookies.

C#
var theme = await cookie.Get("theme");        // ButilCookie? (Name + Value only)

var value = await cookie.GetValue("theme");   // string?

var all = await cookie.GetAll();              // ButilCookie[]
Live sample
Name
read output
Results will appear here when you interact with the samples.
Warning:
HttpOnly cookies are invisible Cookies flagged HttpOnly by the server never appear in document.cookie, so neither Get nor GetAll can see them. That is by design - it protects session tokens from script access. Use them for anything security-sensitive.
Note:
Attributes are write-only The browser returns only name=value pairs when reading cookies. A ButilCookie you get back therefore has default values for Domain, Expires, MaxAge, Path, SameSite, Secure and Partitioned - regardless of how it was originally set. If you need full cookie metadata, use the CookieStore API instead.

API reference

Member
Signature
Description
GetAll
Task<ButilCookie[]> GetAll()
Returns every cookie registered on the current document. Only Name and Value are populated.
Get
Task<ButilCookie?> Get(string name)
Returns the cookie with the given name, or null when absent.
GetValue
Task<string?> GetValue(string name)
Returns the value of the named cookie, or null when absent.
Set
Task Set(ButilCookie cookie)
Writes the cookie to the document, serializing all populated attributes.
Remove
Task Remove(string name)
Removes a cookie by name (sets MaxAge = 0).
Remove
Task Remove(ButilCookie cookie)
Removes a cookie, matching its Path/Domain attributes when they were used to set it.
ButilCookie
class ButilCookie { string? Name; string? Value; string? Domain; DateTimeOffset? Expires; long? MaxAge; bool Partitioned; string? Path; SameSite? SameSite; bool Secure; }
Strongly-typed cookie model covering every Set-Cookie attribute.
ButilCookie.ToString
string ToString()
Serializes the cookie to document.cookie syntax with percent-encoded name and value.
ButilCookie.Parse
static ButilCookie? Parse(string rawCookie)
Parses a raw name=value pair into a ButilCookie; returns null for invalid input.
SameSite
enum SameSite { None, Lax, Strict }
Controls when the browser sends the cookie on cross-site requests.
An unhandled error has occurred. Reload 🗙