Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor does not show content after connection resumes after interruption

$
0
0

I have written a web app using Windows Authentication. It uses the new(ish) Blazor Web App template, which means that there is a constant SignalR connection between server and client.

I found a scenario where the connection can be interrupted. When this happens it automatically reconnects however it now thinks it is unauthorized (e.g. where I am using an <AuthorizeView> component).

To try and figure out why I've implemented a CircuitHandler, as follows:

public class UserCircuitHandler : CircuitHandler{    private AuthenticationStateProvider authenticationStateProvider;    public UserCircuitHandler(AuthenticationStateProvider authenticationStateProvider)    {        this.authenticationStateProvider = authenticationStateProvider;        authenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProvider_AuthenticationStateChanged;    }    public override Task OnCircuitClosedAsync(Circuit circuit, CancellationToken cancellationToken)    {        return base.OnCircuitClosedAsync(circuit, cancellationToken);    }    public override Task OnCircuitOpenedAsync(Circuit circuit, CancellationToken cancellationToken)    {        return base.OnCircuitOpenedAsync(circuit, cancellationToken);    }    public override Task OnConnectionDownAsync(Circuit circuit, CancellationToken cancellationToken)    {        return base.OnConnectionDownAsync(circuit, cancellationToken);    }    public override async Task OnConnectionUpAsync(Circuit circuit, CancellationToken cancellationToken)    {         await base.OnConnectionUpAsync(circuit, cancellationToken);    }    private async void AuthenticationStateProvider_AuthenticationStateChanged(Task<AuthenticationState> task)    {        //If I put a break point here, I can see that it does re-authenticate successfully.    }}

And then register it in Program.cs as follows:

services.AddScoped<CircuitHandler, UserCircuitHandler>();

If I put break-points in this, I can see that it detects when the connection goes down, comes back up, and the AuthenticationStateProvider event fires a couple of times, by the end of which it indicates that it has re-authenticated successfully. But yet the UI does not show the content of the <AuthorizeView>. Why not?

I should add that in the Output section (Visual Studio 2022) I see the following appear a few times:

Microsoft.AspNetCore.Authorization.DefaultAuthorizationService: Information: Authorization failed. Fail() was explicitly called.

For the record and in case anyone else experiences similar issues, the issue that breaks the connection is easily fixable. This was caused by SignalR receiving too much data at once over JS interop (default maximum is 32KB), and can be fixed as follows in Program.cs:

builder.Services.Configure<HubOptions>(options =>{    options.MaximumReceiveMessageSize = null;});

I am just concerned that should a future scenario result in the same outcome, authentication is not automatically re-established.

Any advice is much appreciated.


Viewing all articles
Browse latest Browse all 4839

Trending Articles