In Blazor Server App, getting 404 error on page refresh

Mrbanad 30 Reputation points
2024-03-11T06:57:18.34+00:00

I am working on a Blazor server app with ASP.NET Core MVC and MudBlazor for design. My panel pages are in a PanelProject.

I face the following issues:

  1. Whenever I try to refresh a page, I get a 404 error.
  2. Sometimes, I get a 403 error without knowing why.
  3. On encountering an exception, the Blazor app stops working, and I have to refresh the page.

Can someone help me find a solution?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,422 questions
{count} vote

2 answers

Sort by: Most helpful
  1. Mrbanad 30 Reputation points
    2024-03-25T05:21:04.8466667+00:00

    problem is I have an attribute In the Root Folder Of My AdminPanel and one in the Child Folder for Example

    Root:

    @attribute [Authorize(Roles = "Administrator, System")]
    

    Child:

    @attribute [Authorize(Roles = "Branch")]
    

    I just changed my

    @attribute [Authorize(Roles = "Branch")]
    

    to

    @attribute [Authorize(Roles = "Administrator, System, Branch")]
    
    1 person found this answer helpful.
    0 comments No comments

  2. Rely 0 Reputation points
    2024-05-13T08:24:22.2833333+00:00

    I also have the same issue. After countless repeated tests, I finally discovered that the problem in the custom AuthenticationStateProvider not being triggered.

    @page "/myform02"
    @attribute [Authorize(Roles = "MYFORM02")]  //---* Authz FAIL when refreshing the page with F5.
    
    <MudContainer>
      <MudText Typo=Typo.h2>MyForm02</MudText>
    </MudContainer>
    

    Because when pressing F5 to refresh the page, the CustomAuthenticationStateProvider is not triggered => invalid authorization => (Unknown handling action...) => 404

    using Microsoft.AspNetCore.Components.Authorization;
    using System.Security.Claims;
    
    namespace N8BlazorServerAuth.Services;
    
    internal class CustomAuthenticationStateProvider(IHttpContextAccessor _http) : AuthenticationStateProvider
    {
      public override Task<AuthenticationState> GetAuthenticationStateAsync()
      {
        var userIdentity = _http.HttpContext?.User.Identity as ClaimsIdentity;
        if (userIdentity == null)
        {
          var anonymous = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
          return Task.FromResult(anonymous);
        }
    
        //---* Added authorization
        userIdentity.AddClaim(new Claim(ClaimTypes.Role, "MYFORM02"));
    
        // Success
        return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(userIdentity)));
      }
    }
    

    in NET5 NET6, pressing F5 to refresh the page is fine, but NET8 it's FAIL!

    I had register

    builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthenticationStateProvider>();