Skip to content

ASP.Net Core 3.0 SignalR WebSocket connection failure #14781

@khteh

Description

@khteh

If you believe you have an issue that affects the security of the platform please do NOT create an issue and instead email your issue details to secure@microsoft.com. Your report may be eligible for our bug bounty but ONLY if it is reported through email.

Describe the bug

A clear and concise description of what the bug is.
I follow the tutorial in https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-3.0&tabs=visual-studio-code and fails to establish WebSocket connections. My same application works very well when using ASP.Net Core 2.2.
In Startup.cs:
ConfigureServices:

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(configureOptions =>
            {
                    OnMessageReceived = async (context) =>
                    {
                        if (context.Request.Path.StartsWithSegments("/chatHub"))
                        {
                            string accessToken = context.Request.Query["access_token"];
                            if (string.IsNullOrEmpty(accessToken) && context.Request.Headers.ContainsKey("Authorization"))
                            {
                                accessToken = context.Request.Headers["Authorization"];
                                accessToken = accessToken.Split(" ")[1];
                            }
                            if (!string.IsNullOrEmpty(accessToken))
                                context.Token = accessToken;
                        }
                    }
             };
         });

Configure():

app.UseWebSockets();

In chat.js:

var connection = new signalR.HubConnectionBuilder().withUrl(pathBase+"/chatHub", {
    skipNegotiation: true,
    transport: signalR.HttpTransportType.WebSockets,
    accessTokenFactory: () => token
}).configureLogging(signalR.LogLevel.Trace).build();

When I set the skipNegotiation to true, this is what I get:
SignalRWithoutTransportNegotiationFailure
and the OnMessageReceived event is NEVER triggered with the /chatHub path.

When I set the skipNegotiation to false, this is what I get:
SignalRWithTransportNegotiationFailure
and the OnMessageReceived event IS triggered with the /chatHub path.

When I leave out the skipNegotiation and transport options but only set the accessTokenFactory, this is what I get:
SignalRWithDefaultOptionsFailure
and the OnMessageReceived event IS triggered with the /chatHub path.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core '...' 3.0
  2. Run this code '....'
  3. With these arguments '....'
  4. See error

Expected behavior

A clear and concise description of what you expected to happen.
Is there anything change in 3.0 SignalR? The same application is running with 2.2.

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.
Include the output of dotnet --info

$ dn --info
.NET Core SDK (reflecting any global.json):
 Version:   3.0.100
 Commit:    04339c3a26

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  19.04
 OS Platform: Linux
 RID:         ubuntu.19.04-x64
 Base Path:   /usr/share/dotnet-3.0.100/sdk/3.0.100/

Host (useful for support):
  Version: 3.0.0
  Commit:  7d57652f33

.NET Core SDKs installed:
  3.0.100 [/usr/share/dotnet-3.0.100/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.0.0 [/usr/share/dotnet-3.0.100/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.0.0 [/usr/share/dotnet-3.0.100/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions