https://github.com/soenneker/soenneker.blazor.utils.cookies
A Blazor interop utility for cookie management
https://github.com/soenneker/soenneker.blazor.utils.cookies
blazor blazorlibrary cookie cookies cookiesutil cookieutil csharp dotnet utils
Last synced: 2 months ago
JSON representation
A Blazor interop utility for cookie management
- Host: GitHub
- URL: https://github.com/soenneker/soenneker.blazor.utils.cookies
- Owner: soenneker
- License: mit
- Created: 2026-03-18T12:40:04.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2026-04-20T20:16:13.000Z (2 months ago)
- Last Synced: 2026-04-20T22:22:42.687Z (2 months ago)
- Topics: blazor, blazorlibrary, cookie, cookies, cookiesutil, cookieutil, csharp, dotnet, utils
- Language: CSS
- Homepage: https://soenneker.com
- Size: 192 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/soenneker.blazor.utils.cookies/)
[](https://github.com/soenneker/soenneker.blazor.utils.cookies/actions/workflows/publish-package.yml)
[](https://www.nuget.org/packages/soenneker.blazor.utils.cookies/)
[](https://soenneker.github.io/soenneker.blazor.utils.cookies)
# Soenneker.Blazor.Utils.Cookies
Blazor library for reading, writing, removing, and listing browser cookies. Exposes a low-level `ICookiesInterop` for direct browser interop and a higher-level `ICookiesUtil` for app-facing usage.
---
## Install
```bash
dotnet add package Soenneker.Blazor.Utils.Cookies
```
---
## Setup
Register cookie services in `Program.cs`:
```csharp
builder.Services.AddCookiesUtilAsScoped();
```
Inject the higher-level utility in components/services:
```csharp
@inject ICookiesUtil Cookies
```
---
## Usage
### Get a cookie
```csharp
string? theme = await Cookies.Get("theme");
```
### Try get a cookie
```csharp
CookieGetResult result = await Cookies.TryGet("theme");
if (result.Found)
{
Console.WriteLine(result.Value);
}
```
### Set a cookie
```csharp
await Cookies.Set("theme", "dark", new CookieOptions
{
Path = "/",
MaxAge = TimeSpan.FromDays(30),
Secure = true,
SameSite = CookieSameSite.Lax
});
```
### Remove a cookie
```csharp
await Cookies.Remove("theme");
```
### Read all cookies
```csharp
IReadOnlyDictionary cookies = await Cookies.GetAll();
foreach ((string key, string value) in cookies)
{
Console.WriteLine($"{key}={value}");
}
```