This issue may be caused by the HTTP request to api/users/me being made too early, before the authentication cookies for API requests are fully set.
To resolve this issue, use a wait and UI update process, as in the sample code below, to have the system wait a while for the logged-in user to be marked as authenticated.
If the error persists, please share the ApiService and UserService codes so I can investigate further and provide further assistance.
@code {
private bool _loaded = false;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && !_loaded)
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity is not null && user.Identity.IsAuthenticated)
{
try
{
await UserService.LoadCurrentUserAsync();
_loaded = true;
StateHasChanged(); // Update UI for get the changes
}
catch (Exception ex)
{
await Console.Error.WriteLineAsync("Error with LoadCurrentUserAsync: " + ex.Message);
}
}
}
}
}