SignalR throws 405 error on notify/negotiate reque

2019-08-05 19:41发布

I connect to the server with angular like this:

    this._hubConnection = new signalR.HubConnectionBuilder()
  .withUrl(location.protocol + '//localhost:5000/notify')
  .configureLogging(signalR.LogLevel.Information)
  .build();

There is no problem with the code or the host. The request works.

But then the library makes an additional call to:

http://localhost:5000/notify/negotiate

Then I get this error:

HTTP ERROR 405

The full error in the client is:

Failed to load http://localhost:5000/notify/negotiate: Response to 

preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

I don't have cors issues in the site apart from the aforementioned request.

I have this configured in the startup.cs.

     app.UseCors(builder =>
              builder.WithOrigins("http://localhost:4200").AllowAnyMethod
 ().AllowAnyHeader().AllowAnyOrigin());

The code works for all the site.

Why that sudden cors error if the rest of the requests work?

1条回答
Summer. ? 凉城
2楼-- · 2019-08-05 19:59

I can suggest you to configure directly web.config file of your webapp adding following lines of code (it works on IIS, not sure for IIS Express):

<system.webServer>
<httpProtocol>
 <customHeaders>
   <add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
   <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS,PUT,DELETE,PATCH" />
   <add name="Access-Control-Allow-Headers" value="Content-Type, X-Requested-With" />
   <add name="Access-Control-Allow-Credentials" value="true" />
 </customHeader></httpProtocol></system.webServer>
查看更多
登录 后发表回答