Blazor Server and SignalR and Azure AD

Viewed 237

I am working on a web application using Blazor Server .Net 5. On my index page, I need to show the number of online users that logged into the website through Azure AD.

First, the user hits the web, and it gets redirected to Azure AD. Once the user is Authenticated in AD he/she will land on the index page. I want to show number of online users inside the app. I started using SignalR, but I am getting a very weird Error.

I am using SingalR client lib First I created the

PeoplHub : Hub{
public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }

}

Then in my Index.razor I have created

hubConnection = new HubConnectionBuilder()
            .WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
            .Build();

        hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
        {
            var encodedMsg = $"{user}: {message}";
            messages.Add(encodedMsg);
            InvokeAsync(StateHasChanged);
        });
         await hubConnection.StartAsync();

I have also Implemented the IAsyncDisposal

 public async ValueTask DisposeAsync()
    {
        if (hubConnection is not null)
        {
            await hubConnection.DisposeAsync();
        }
    }

in my startup I implemented

services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
app.UseResponseCompression();

endpoints.MapHub<PeopleHub>("/peoplehub");

When I run the app, I get this error message

An unhandled exception occurred while processing the request.
JsonReaderException: '<' is an invalid start of a value. LineNumber: 2 | BytePositionInLine: 0.
System.Text.Json.ThrowHelper.ThrowJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte, ReadOnlySpan<byte> bytes)

InvalidDataException: Invalid negotiation response received.
Microsoft.AspNetCore.Http.Connections.NegotiateProtocol.ParseResponse(ReadOnlySpan<byte> content)
1 Answers

After researching on this issue. I found some useful information. We don't know the known issue, you can create a support ticket and ask for help.

It turns out that there is a known issue breaking SignalR Hubs with Blazor Server and Microsoft Identity.

And I also find official engineer said they don't plan to make improvements in this area given that we haven't seen many customers hitting it.

Related Issue:

blazor server signalr JsonReaderException

Workaround

ASP.NET Core Blazor Server additional security scenarios

Related