SignalR 响应覆盖 headers

SignalR responses overwriting headers

我已经构建了一个位于 WebAPI 服务中的简单 SignalR 中心,我已经在 WebAPI 和 SignalR 上包含了所有必需的 CORS 属性。我的 WebAPI 端点都按预期工作,但 SignalR 不是。

我已经尝试了所有我能想到的和我能在网上找到的所有方法,但没有任何效果,我已经尝试 , and this other 没有解决方案。

我的 SignalR 扩展方法如下所示

public static IAppBuilder UseSignalrNotificationService(this IAppBuilder app)
    {
        var config = new HubConfiguration();
        config.Resolver = new HubDependencyResolver();
        config.EnableDetailedErrors = true;
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR(config);

        return app;
    }

我什至尝试使用 Web.config 在所有请求上添加响应 headers 但我总是得到相同的错误:

XMLHttpRequest cannot load https://MyApplicationServer/notifications/signalr/negotiate?clientProtocol=1.5&access_token=&connectionData=. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'MyOriginService' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.

您是否正在使用某种全局拦截器在某处更改请求?出于某种原因,XMLHttpRequest 以 withCredentials:true 开头,当 Access-Control-Allow-Origin 设置为 *.

时禁止这样做

如何将 'Access-Control-Allow-Origin' 设置为“http://MyApplicationServer”?它比 * 更安全,并且会从源头上解决您的问题。

经过更多的研究和摆弄服务器端的问题,我运行变成了this answer and found the error to be with the client side of the request. according to this GitHub issue,请求的"withCredentials"参数总是设置为'true' .解决方案是在客户端调用启动方法,如下所示:

$.connection.hub.start({ withCredentials: false }).done(function () {  //... }