🎓 All Courses | 📚 Blazor WASM Syllabus
Stickipedia University
📋 Study this course on TaskLoco

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.

Using Blazored.LocalStorage Library

// 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");

Manual JS Interop Approach

await JS.InvokeVoidAsync("localStorage.setItem", "key", "value");
string val = await JS.InvokeAsync<string>("localStorage.getItem", "key");

Storage Rules

  • localStorage persists across sessions; sessionStorage clears on tab close
  • Both are limited to ~5MB per origin
  • Never store sensitive data — localStorage is accessible via JavaScript and vulnerable to XSS
  • Use short-lived JWT tokens stored in memory when security is critical

YouTube • Top 10
Blazor WASM: Local Storage and Session Storage
Tap to Watch ›
📸
Google Images • Top 10
Blazor WASM: Local Storage and Session Storage
Tap to View ›

Reference:

Wikipedia: Web Storage

image for linkhttps://en.wikipedia.org/wiki/Web_storage

📚 Blazor WASM — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT