🟨 Slightly different to Blazor WebAssembly
Cookie storage is a mechanism used by web browsers to store small pieces of data that can be sent back to a web server along with subsequent requests.
This tutorial provides a basic setup for interacting with Cookie storage, which can be extended with additional features as needed. By following this tutorial, you will learn:
You can download the example code used in this topic on GitHub.
The Cookie Storage allows you to store information in key-value pairs. Previously, cookies were a cross-domain resource, but starting in 2023, cookies will be bound to a domain. Each key-value pair in cookies has an expiry date, which can be set to "forever" if desired. However, it is important to note that users may delete cookies, and cookies can be made inaccessible from JavaScript code.
Despite the advantages of using Cookie Storage, we do not recommend it for the following reasons:
MDN recommends against using Cookie storage, as stated below:"Cookies were originally used for general client-side storage. While this made sense when they were the only way to store data on the client, modern storage APIs are now recommended. Cookies are sent with every request, so they can worsen performance (especially for mobile data connections). Modern APIs for client storage are the Web Storage API (localStorage and sessionStorage) and IndexedDB."
Source Using HTTP cookies
In Firefox, you can find the Cookie storage under the Storage tab, as shown in the image below:
In Chrome and Edge, you can find the Cookie storage under the Application tab, as shown in the images below:
The Cookie storage is exclusively accessible through the JavaScript API. This means that in order to access it, you will need to create a JavaScript module. You can refer to the JavaScript Interaction tutorial and choose IJSRuntime
to interact with your module. Below is an example of how to access the Cookie storage using IJSRuntime
.
public class CookieStorageAccessor { private Lazy<IJSObjectReference> _accessorJsRef = new(); private readonly IJSRuntime _jsRuntime; public CookieStorageAccessor(IJSRuntime jsRuntime) { _jsRuntime = jsRuntime; } private async Task WaitForReference() { if (_accessorJsRef.IsValueCreated is false) { _accessorJsRef = new(await _jsRuntime.InvokeAsync<IJSObjectReference>("import", "/js/CookieStorageAccessor.js")); } } public async ValueTask DisposeAsync() { if (_accessorJsRef.IsValueCreated) { await _accessorJsRef.Value.DisposeAsync(); } } }
CookieStorageAccessor
class in the Program.cs file:builder.Services.AddScoped<CookieStorageAccessor>();
In this section, we will demonstrate how to perform basic Cookie storage operations in JavaScript and C#. With the provided code, you can easily store and get data from the cookies. Additionally, you can customize the code to add your own operations as well.
export function get() { return document.cookie; } export function set(key, value) { document.cookie = `${key}=${value}`; }
WaitForReference()
in all of them. Here is an example implementation:public class CookieStorageAccessor { ... public async Task<T> GetValueAsync<T>(string key) { await WaitForReference(); var result = await _accessorJsRef.Value.InvokeAsync<T>("get", key); return result; } public async Task SetValueAsync<T>(string key, T value) { await WaitForReference(); await _accessorJsRef.Value.InvokeVoidAsync("set", key, value); } }
In Blazor WebAssembly, you can also use InteropServices.JavaScript
to interact with the JavaScript.