CORS - Response to preflight request doesn't pass access control check
Describe the bug
The application has been working flawlessly in .net core 2.1. The signalR server's configuration looks like the following code snippet in public void ConfigureServices(IServiceCollection services) method:
services.AddCors(setupAction =>
setupAction.AddPolicy("MyPolicy",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()))
And in Configuration method I have the following code snippet:
app.UseCors("MyPolicy")
The angular client app's code starts the connection like the following code snippet:
this._connection = this._connection || new HubConnectionBuilder()
.withUrl('https://localhost:44314/xyz/test?abc=123', options)
.build();
The signalR client's version is 1.1.0 in both angular app and the C# client application.
To Reproduce
As I said, this setup has always been working fine. I upgraded the .net core application (signalR server) from 2.1 to 2.2 (dotnetcore SDK ver 2.2.101) and I upgraded the signalR client package to 1.1.0. The signalR server serves an angular app and another C# application. The C# application connects to the signalR server perfectly and it does whatever it's supposed to do but the angular app produces the following error message:
Access to XMLHttpRequest at 'https://localhost:44314/xyz/test/negotiate?abc=123' from origin 'https://localhost:44303' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Expected behavior
The application should still keep working after migration from .net core 2.1 to 2.2
Additional context
dotnet version is 2.2.101
https://github.com/aspnet/AspNetCore/issues/4457#issuecomment-444738776
@dallancarr Thanks David, that link / comment is a long thread and it also points to another stackoverflow thread. which part should I look at specifically? Composing a middleware?
Certainly an option..but if you can configure using cors then I imagine less error prone.
You need to explicitly specify your allowed origins.
@dallancarr It worked, the technique was as you said specifying the allowed origins explicitly. Thanks and hence I am closing this issue.
Same flat. Works perfect ASP.NET Core 2.1 (just as you described it), then a big problem when update to 2.2. This workaround works:
readonly string allowSpecificOrigins = "AllowAllHeaders";
// ...
// ConfigureServices
var origins = new string[] {
"http://localhost:50768", // development
"http://localhost/TM", // IIS
"http://190.159.143.xxx/TM", // public IP
// others
};
services.AddCors(options => {
options.AddPolicy(allowSpecificOrigins,
builder => {
builder.WithOrigins(origins)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
// Configure...
app.UseCors(allowSpecificOrigins);
app.UseMvc();
// SignalR (the middleware pipeline)
app.UseSignalR(routes => {
routes.MapHub<OnlineRecordHub>("/broadcastingHub");
});
But when I do the publication on the production server, I go back to get the damn:
...has been blocked by CORS policy: Response to preflight request doesn't pass access control No 'Access-Control-Allow-Origin' header is present on the requested resource._
I would not like to go back to 2.1 - Any suggestions from the experts?