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

In Blazor .NET 8 , where do I load and store user details?

$
0
0

In ASP.NET MVC, I handled the session_start event to load user details and store them in a session variable, so I didn't have to query the database with each page load.

I'm trying to figure out how to do this in Blazor on .NET 8.

I seams like the App.razor is a good place to put my code, since it is executed with each page load - but I don't know if it is the best practice?

My code in App.razor (server code) looks like this (I'm using Windows authentication):

@code {    protected override async Task OnInitializedAsync()    {        UserDetails details;        var user = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User;        var userId = user.Identity?.Name?.ToLower();        var result = await BrowserStorage.GetAsync<UserDetails>(userId);        if (result.Success)        {            details = (UserDetails)result.Value;        }        else        {            GetUserDetailsQuery query = new GetUserDetailsQuery();            query.UserLogin = await getUserId();            details = query.Handle();            await BrowserStorage.SetAsync(userId, details);        }       }}

But I get an error:

InvalidOperationException: JavaScript interop calls cannot be issued at this time.

How do you guys solve this? Do you query the database for the same info on each page load?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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