SignalR响应覆盖标头

时间:2016-04-14 22:49:04

标签: asp.net-web-api signalr owin

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

我已经尝试了所有我能想到的和我在网上找到的所有内容但没有任何作用,我已经尝试this answerthis 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在所有请求上添加响应标头,但我总是得到同样的错误:

  

XMLHttpRequest无法加载https://MyApplicationServer/notifications/signalr/negotiate?clientProtocol=1.5&access_token=& connectionData =。通配符' *'不能用于“访问控制 - 允许 - 来源”#39;凭证标志为true时的标头。起源' MyOriginService'因此不允许访问。 XMLHttpRequest的凭证模式由withCredentials属性控制。

2 个答案:

答案 0 :(得分:13)

经过更多的研究和摆弄问题的服务器端,我遇到this answer并发现错误与请求的客户端有关。根据{{​​3}}," withCredentials"请求的参数始终设置为' true'。解决方案是在客户端上调用start方法,如下所示:

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

答案 1 :(得分:0)

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

设置' Access-Control-Allow-Origin'到' http://MyApplicationServer'?它比*更安全,并将从源头上删除您的问题。