
Blazor WASM runs in the browser but C# cannot access browser storage APIs directly. You need JS interop or a library wrapper to read and write localStorage and sessionStorage.
// Install: dotnet add package Blazored.LocalStorage
// Program.cs:
builder.Services.AddBlazoredLocalStorage();
// Component:
@inject ILocalStorageService LocalStorage
private async Task SaveToken(string token)
=> await LocalStorage.SetItemAsync("auth_token", token);
private async Task<string> GetToken()
=> await LocalStorage.GetItemAsync<string>("auth_token");await JS.InvokeVoidAsync("localStorage.setItem", "key", "value");
string val = await JS.InvokeAsync<string>("localStorage.getItem", "key");Reference:
TaskLoco™ — The Sticky Note GOAT