redar ismail
redar ismail

Reputation: 171

Blazor Server and SignalR and Azure AD

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)

Upvotes: 5

Views: 1411

Answers (2)

JimiSweden
JimiSweden

Reputation: 814

Adding on to the answer by Jason Pan.

A quick way to validate the authorization is the problem.

Since I knew my code worked without Authorization in a dotnet 7 app and this error was seen when I moved the code into my production code (dotnet 6) where we use authorization with Azure AD

I ran a test with "AllowAnymous" on the hub.

[AllowAnonymous()] //TODO: authorize...
    public class SignalrHub : Hub
    {

and everything works as expected. Next : follow the workaround as posted by Jason

Upvotes: 2

Jason Pan
Jason Pan

Reputation: 21838

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

Upvotes: 4

Related Questions