juststressed
juststressed

Reputation: 89

What is the best practice to implement user session variables in Blazor Server applications?

I'm teaching myself to program, I'm experimenting with a Blazor Server application using c# and asp.net core.

Take the hypothetical class Workspace:

public List<Item> Items { get; set; }

Each user may be associated to one-to-many instances of Workspace; this will authorise them to select one of said Workspace instances and perform create, read, update, and delete operations on the child Item instances (i.e. Workspace.Items). Note, these operations may be performed on different pages.

What is the best way to store a reference to the current Workspace? e.g. should I store the current Workspace as a property in a User account? or maybe there is a better way of recording the session state?

Upvotes: 2

Views: 6841

Answers (1)

dani herrera
dani herrera

Reputation: 51715

On Blazor Server you can store state data in several places, is full documented at ASP.NET Core Blazor state management. Summary:

  • Server-side storage For permanent data persistence that spans multiple users and devices, the app can use server-side storage. Options include: Blob storage, Key-value storage, Relational database, Table storage
  • URL: For transient data representing navigation state, model the data as a part of the URL.
  • Browser storage: For transient data that the user is actively creating, a commonly used storage location is the browser's localStorage and sessionStorage collections.
  • In-memory state container service: Nested and un-nested components can share access to data using a registered in-memory state container. A custom state container class can use an assignable Action to notify components in different parts of the app of state changes.

Upvotes: 4

Related Questions