2016-04-14 73 views
6

我已经构建了一个简单的SignalR中心,它位于WebAPI服务中,我在WebAPI和SignalR中都包含了所有必需的CORS属性。我的WebAPI端点都按预期工作,但SignalR不是。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 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.

回答

13

经过更多的研究和摆弄问题的服务器端,我碰到了this answer,发现错误与客户端的请求。根据this GitHub issue,请求的“withCredentials”参数始终设置为“true”。解决的办法是在客户端上调用start方法如下:

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

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

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

+0

我试图改变SignalR的CORS策略指定的起源没有结果,一些奇怪的与请求和其头发生 – evilpilaf