I have created a mudblazor application and created an cshtml page as authentication provider and into it .CS file I have declared all logic to get jwt token response and their roles. Now I am trying to set the same using below:-
Httpcontext.Session.SetString("authentication", "true");
But if I try to use that httpcontext on different pages like my navmenu.razor page or any other razor page this httpcontext is not accessible.
I tested with using IHttpContextAccessor
and it works to access session in different components.
program.cs
builder.Services.AddHttpContextAccessor();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession();
...
app.UseSession();
Home.razor
@inject IHttpContextAccessor accessor
@code{
protected override void OnInitialized()
{
accessor.HttpContext.Session.SetString("authentication", "true");
}
}
Counter.razor
@inject IHttpContextAccessor accessor
authentication:
@accessor.HttpContext.Session.GetString("authentication")