Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4175

Dependency injection in Blazor does not work while switching page

$
0
0

I have a class

public class SessionState {    public int id { get; set; }    public bool verified { get; set; }}

And in every page I have injected the class with @inject SessionState state.

I want to fill state.id while the user login, I wrote this code:

state.id = some value;state.verified = true;NavigationManager.NavigateTo("home");

And I have this code in Program.cs:

builder.Services.AddScoped<SessionState>();

Now when the web site navigates to Home, I have no value in state.id.

I also used this:

builder.Services.AddSingleton<SessionState>();

but the problem is when user 1 logs in I have state.id = 1, after user1 user2 logs in and I have state.id = 2

How can I have state.id unique for every user that logs in, and I need it in all of my Blazor pages.

state.id = int.Parse(dt.Rows[0]["iduser"].ToString());state.verified = true;NavigationManager.NavigateTo("home");

Viewing all articles
Browse latest Browse all 4175


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>