Paul
Paul

Reputation: 1006

ASP.net core signalr angular client, error "Response to preflight request doesn't pass access control check"

So I have this code in ASP.Net core server startup.cs in ConfigureServices method :

services.AddCors(o => o.AddPolicy("CorsPolicy", builder => {
            builder
            .AllowAnyMethod()
            .AllowAnyHeader()
            .WithOrigins("https://localhost:44345");
        }));

And within Configure method :

app.UseCors("CorsPolicy");

In angular I have this :

  ngOnInit(): void

{

this._hubConnection = new signalR.HubConnectionBuilder().withUrl('https://localhost:44305/hub').build();
this._hubConnection
  .start()
  .then(() => console.log('Connection started!'))
  .catch(err => console.log('Error while establishing connection :('));

}

The client part and server part will run on different domains, they are 2 separate projects in visual Studio.

Anyway when running the angular code i get this error :

Failed to load https://localhost:44305/hub/negotiate: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'https://localhost:44345' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

So I guess I have 2 options, either get the client side code to pass false for the withCredentials attribute or somehow change the server side code do it sends a Access-Control-Allow-Credentials response header set to true. The problem is I don't know how to do either of those things, I can't find a setting for withCredentials anywhere.

Upvotes: 2

Views: 683

Answers (1)

Paul
Paul

Reputation: 1006

I have solved it, i needed to include .AllowCredentials() in the services.addCors part.

Upvotes: 2

Related Questions