question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SignalR/CORS: Credential is not supported if the CORS header ‘Access-Control-Allow-Origin’ is ‘*’

See original GitHub issue

Describe the bug

Hi! I just upgraded from ASP.NET Core 2.1.6 to 2.2.0, everything working just fine except my connections to SignalR. In the negotiation phase (using javascript client and over websocket) I get a 204 with the error

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ‘https://localhost:44333/ChatHub/negotiate’. (Reason: Credential is not supported if the CORS header ‘Access-Control-Allow-Origin’ is ‘*’).

Taking a look at the request I have the following

headers

So the server sends out a ‘*’ instead of just sending out the origin from which the request came. The app needs to allow all origins since our users can add a chat to their own site.

In my Startup.cs I have defined CORS rules as follows

services.AddCors(options => { options.AddPolicy("AllowAllOrigins", b => { b.AllowAnyOrigin() .AllowCredentials() .AllowAnyHeader() .AllowAnyMethod(); }); });

Mind that this was working on 2.1.6 and earlier versions.

In the client side code I can get the chat to work if I set skipNegotiation to true, like this connection = new signalR.HubConnectionBuilder() .withUrl(serverURL + "ChatHub", { skipNegotiation: true, transport: signalR.HttpTransportType.WebSockets }) .build(); But I’m guessing that will have it’s own consequences and is not solving the real problem.

Been reading through so many threads and I just can’t get this to work again, anyone have any ideas as to might be causing it?

To Reproduce

Steps to reproduce the behavior:

  1. Using ASP.NET Core 2.2.0
  2. Create a Hub, add CORS settings and use a JS client to try to connect from a origin other then where the server is running

Expected behavior

To not recieve the CORS error, instead have the server return the allowed origin as a URL instead of wildcard ‘*’ since this won’t work with credentials, which is needed for SignalR as I understand it (for sticky cookies)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

29reactions
solojuve1897commented, Feb 20, 2019

@cores-system Thanx for the answer. What I ended up doing was the following:

In ConfigureServices-method:

services.AddCors(options => options.AddPolicy("CorsPolicy",
            builder =>
            {
                builder.AllowAnyHeader()
                       .AllowAnyMethod()
                       .SetIsOriginAllowed((host) => true)
                       .AllowCredentials();
            }));

In Configure-method:

app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
            {
                routes.MapHub<General>("/hubs/general");
            });
7reactions
roos-robertcommented, Dec 6, 2018

Also, since setting skipNegotiation to true “solves” the problem, what does that actually do? With it set to true chatting works fine - does that mean the CORS settings are working overall, but just not for the negotiation part?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cors with SignalR - solving the "The value of the 'Access- ...
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is ...
Read more >
Reason: Credential is not supported if the CORS header ...
The CORS request was attempted with the credentials flag set, but the server is configured using the wildcard ( "*" ) as the...
Read more >
SignalR CORS error in .NET 5.0 C# | by Dee Nguyen
SignalR CORS error in .NET 5.0 C#. Try the configuration below if you have the following CORS (Cross-Origin Resource Sharing) related errors from...
Read more >
“Access-Control-Allow-Origin” – A Savior For Cross ...
In our case we got below error in IE when we were trying to establish cross domain hub connection from client side using...
Read more >
Reason: expected 'true' in CORS header 'Access-Control ...
The CORS request requires that the server permit the use of credentials, but the server's Access-Control-Allow-Credentials header's value isn't set to true ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found