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?