dotnet / dotnet/aspnetcore

Howto : .Net8 Authenticated (Azure) SignalR with Blazor Server

Open
#54,134 19 comments 0 reactions 0 assignees View on GitHub
area-blazor Blazor ♥ SignalR reevaluate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Describe the bug

How do you correctly setup SignalR with authentication on Blazor Server? I created a test project based on the BlazorWebAppOidc:

https://github.com/njannink/BlazorServerAppOidcWithHub/tree/main

Fill in your OIDC + Azure SignalR details in the `BlazorWebAppOidc/Program.cs` file, or you can also turn off Azure SignalR to use the default SignalR hosting model. Everything is working fine when using the InteractiveAuto hosting model, but as soon as you switch to the InteractiveServer way the SignalR connection can no longer be established.

If you then login and try to connect to the chat hub you get the following error:

```
System.IO.InvalidDataException: Invalid negotiation response received.
---> System.Text.Json.JsonReaderException: '<' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.
at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker)
at System.Text.Json.Utf8JsonReader.ReadFirstToken(Byte first)
at System.Text.Json.Utf8JsonReader.ReadSingleSegment()
at System.Text.Json.Utf8JsonReader.Read()
at Microsoft.AspNetCore.Internal.SystemTextJsonExtensions.CheckRead(Utf8JsonReader& reader)
at Microsoft.AspNetCore.Http.Connections.NegotiateProtocol.ParseResponse(ReadOnlySpan`1 content)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Http.Connections.NegotiateProtocol.ParseResponse(ReadOnlySpan`1 content)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.NegotiateAsync(Uri url, HttpClient httpClient, ILogger logger, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.GetNegotiationResponseAsync(Uri uri, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.SelectAndStartTransport(TransferFormat transferFormat, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsyncCore(TransferFormat transferFormat, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnection.StartAsync(TransferFormat transferFormat, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionFactory.ConnectAsync(EndPoint endPoint, CancellationToken cancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncCore(CancellationToken cancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsyncInner(CancellationToken cancellationToken)
at Microsoft.AspNetCore.SignalR.Client.HubConnection.StartAsync(CancellationToken cancellationToken)
at BlazorWebAppOidc.Client.Pages.Chat.OnInitializedAsync() in D:\Projects\blazor\BlazorServerAppOidcWithHub\BlazorWebAppOidc.Client\Pages\Chat.razor:line 43
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost: Error: Unhandled exception in circuit '80pb6dJz85Pl8JrsLdYsB0wQ26jpH6Mi-79i_fPlXZg'.
```

Any help would be greatly appreciated, since we won't be able to switch to Auto/WebAssembly mode anytime soon. I know there is a way to get the access_token from the HttpContext, but as soon as you use Azure SignalR the HttpContextAccessor.HttpContext returns null.

cc: @guardrex https://github.com/dotnet/blazor-samples/issues/204

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.