2016-04-14 75 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