.AuthService.Logout() is with status code does not idicate success 500

sblb 1,231 Reputation points
2023-06-14T22:05:17.8+00:00

Hi, I Tried to implement an Authentication in my app all things works except LogOut.

I follow up this tutorial : https://codewithmukesh.com/blog/authentication-in-blazor-webassembly/#Authentication_State_Provider

When I click on Logout I received the message :

System.Net.Http.HttpRequestException: Response status code does not indicate success: 500 (Internal Server Error).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at FollowUpDash.Client.Services.AuthService.Logout() in C:\App\sbd\FollowUpDash\FollowUpDash\Client\Services\IAuthService.cs:line 36
   at FollowUpDash.Client.Services.CustomStateProvider.Logout() in C:\App\sbd\FollowUpDash\FollowUpDash\Client\Services\CustomStateProvider.cs:line 41
   at FollowUpDash.Client.Shared.MainLayout.LogoutClick() in C:\App\sbd\FollowUpDash\FollowUpDash\Client\Shared\MainLayout.razor:line 43



IAuthService.cs line 36

   public async Task Logout()
        {
            var result = await _httpClient.PostAsync("api/auth/logout", null);
            result.EnsureSuccessStatusCode();
        }

CustomStateProvder.cs line 41

  public async Task Logout()
        {
            await api.Logout();
            _currentUser = null;
            NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
        }

MainLayout.razor line 43

  async Task LogoutClick()
    {
        await authStateProvider.Logout();
        navigationManager.NavigateTo("/login");
    }

I don't know to fix this problem. Have you an idea?

Developer technologies .NET Blazor
{count} votes

1 answer

Sort by: Most helpful
  1. sblb 1,231 Reputation points
    2023-06-15T13:31:10.4233333+00:00

    Hi, I fix my problem.

    The problem was in Program.cs

    I've put app.UseRouting();

    before

    app.UseAuthentication();

    app.UseAuthorization();

    After this change Logout works.

    Thanks to your support.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.