当请求的凭据模式为“include”时,响应中“Access-Control-Allow-Credentials”标头的值为“'必须为”true“

时间:2017-08-05 18:44:01

标签: angularjs azure signalr asp.net-web-api2 azure-web-sites

我正在开发一款应用,我的服务器在Azure平台上作为Api App运行,我想使用signalR我收到此错误

XMLHttpRequest cannot load http://sitename.net/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%5D&_=1501957673711. The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://localhost:35609' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

我的服务器即时通讯使用owin启动,这是配置

 app.Map("/signalr", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration {};
            hubConfiguration.EnableJSONP = true;
            map.RunSignalR(hubConfiguration);

        });

在我的webConfig中我尝试了这个

var cors = new EnableCorsAttribute("*", "*", "*")
        {
            SupportsCredentials = true
        };
        config.EnableCors(cors);

但它没有解决错误。 如果有人知道我缺少什么请帮助..谢谢

在我的客户端我使用像这样的angularjs

 var connection = null;
this.initialize = function () {
    connection = $.hubConnection("http://name.azurewebsites.net/signalr"{ useDefaultPath: false });
    this.proxy = connection.createHubProxy('myHub');
   connection.start().done(function () {
        console.log("started");
    }).fail(function (result) {
        console.log(result);
    });
};

在我的azure api app中,我已将我的本地域添加到允许的CORS中。我可以做其他事情,但我不能使用signalR

1 个答案:

答案 0 :(得分:0)

我们可以从Azure门户设置Azure App Service的CORS。更多详细信息请参阅How to configure CORS in Azure App Service

  

App Service为跨源资源共享(CORS)提供内置支持,使得JavaScript客户端能够对API应用程序中托管的API进行跨域调用

文档截图:

enter image description here