Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR Core 2.2 CORS AllowAnyOrigin() breaking change

To connect via SignalR to an ASP.NET Core 2.1 server from any origin, we had to configure the pipeline as follows:

app.UseCors (
  builder => builder
   .AllowAnyHeader ()
   .AllowAnyMethod ()
   .AllowAnyOrigin ()
   .AllowCredentials ()
)

According to this document, ASP.NET Core 2.2 no longer allows the combination of AllowAnyOrigin and AllowCredentials, so what would be the solution? Whereas the SignalR Core always sends withCredentials:true in the XMLHtppRequest.

What I need is that from any origin and without credentials, our users can connect to the SignalR Hub.

like image 721
Alexandre Avatar asked Dec 14 '18 21:12

Alexandre


People also ask

Why does SignalR disconnect?

A SignalR connection can end in any of the following ways: If the client calls the Stop method, a stop message is sent to the server, and both client and server end the SignalR connection immediately.

Is SignalR obsolete?

SignalR is deprecated. May I know the latest package for the same. - Microsoft Q&A.

How many connections SignalR can handle?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

Does SignalR require sticky session?

SignalR requires that all HTTP requests for a specific connection be handled by the same server process. When SignalR is running on a server farm (multiple servers), "sticky sessions" must be used. "Sticky sessions" are also called session affinity by some load balancers.


2 Answers

There is a workaround, change AllowAnyOrigin to SetIsOriginAllowed:

app.UseCors(builder => builder
                .AllowAnyHeader()
                .AllowAnyMethod()
                .SetIsOriginAllowed(_ => true)
                .AllowCredentials()
            );
like image 67
Alexandre Avatar answered Oct 04 '22 22:10

Alexandre


I have found a solution. You can try the following code part:

.SetIsOriginAllowed (_ => true)

This worked for me.

like image 36
Ozgur Aslan Avatar answered Oct 04 '22 23:10

Ozgur Aslan