AuthorizeView rendering/resolving before auth state updates, but updates on refresh

I have a server app that connects to a custom login server, receives a JWT, and stores it in session storage for use throughout the app. I use <AuthorizeView> to contain restricted content, but it always resolves to <NotAuthorized> until I refresh the page. After the page is refreshed, the login state is correctly propagated and <Authorized> is visible. This also happens during logout — any authorization change.

I have a custom AuthenticationStateProvider that overrides GetAuthenticationStateAsync(). During this method, I retrieve the auth token from session storage. I am pretty sure that while waiting for this task, the app renders, and doesn’t wait for the authentication state to resolve. Thus, causing the issue. (maybe?)

CustomAuthStateProvider.cs

public override async Task<AuthenticationState> GetAuthenticationStateAsync()
    {        
        var result = await protectedSessionStore.GetAsync<string>("authToken");
        var token = result.Success ? result.Value : string.Empty;
        
        var identity = string.IsNullOrWhiteSpace(token)
            ? new ClaimsIdentity()
            : new ClaimsIdentity(ParseClaimsFromJwt(token), "jwt");
        
        var task = Task.FromResult(new AuthenticationState(new ClaimsPrincipal(identity)));
        NotifyAuthenticationStateChanged(task);
        return await task;
    }

I then inject it like this
Program.cs

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

In Login, I have a form that takes a username/password, sends an HTTP request to the login endpoint, gets a JWT, and authenticates the user with it.

This is where the behavior becomes unexpected. Instead of the components on the screen re-rendering to reflect the new authorization status, they stay the same. Even when I navigate away from the page (via <a>) it doens’t update. It ONLY updates when I refresh.
Login.razor

<AuthorizeView>
    <Authorized>
        @{ Navigation.NavigateTo("/"); }
    </Authorized>
</AuthorizeView>

<EditForm Model="@Model" FormName="Login" OnValidSubmit="HandleLogin">    
    <InputText @bind-Value="Model.Username" placeholder="Username" />
    <InputText @bind-Value="Model.Password" type="password" />
    <button type="submit">Login</button>
</EditForm>

@code {
    [SupplyParameterFromForm]
    public LoginModel Model { get; set; } = new();
    
    private async Task HandleLogin(EditContext obj) => await HandleLoginInternal();

    private async Task HandleLoginInternal()
    {
        var response = await Http.PostAsJsonAsync("User/login", Model);
        if (response.IsSuccessStatusCode)
        {
              var token = await response.Content.ReadAsStringAsync();
              await ProtectedSessionStore.SetAsync("authToken", token);
              await AuthStateProvider.ChangeUser(token);
              StateHasChanged();
        }
    }

    public class LoginModel
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

I’ve noticed that <AuthorizeView> never shows <Authorizing>, leading me to believe that the app thinks it has the authentication state before it actually does. I’d like to fix this issue, as it allows logged in users to access pages they shouldn’t while logged in (for example, the login page)

I’ve tried a lot of disjointed things to get this to work, specifically adding StateHasChanged() to the end of HandleLoginInternal() but it doesn’t do anything. I expect the page to redirect to "/", and the navbar to say “Logout” instead of “Login” upon authorization.

Any ideas? Thanks! I have no idea what I’m doing wrong as I’m new to Blazor and ASP.NET.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật