【问题标题】: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'响应中“Access-Control-Allow-Credentials”标头的值为“”,当请求的凭据模式为“包含”时,该值必须为“真”
【发布时间】:2018-01-13 11:39:00
【问题描述】:

我正在开发一个应用程序,我的服务器在 Azure 平台上作为 Api 应用程序运行,我想使用 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 应用程序中,我已将我的本地域添加到允许的 CORS。我可以做其他事情,但我不能使用signalR

【问题讨论】:

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


【解决方案1】:

我们可以从 Azure 门户为 Azure 应用服务设置 CORS。更多详细信息请参考How to configure CORS in Azure App Service

应用服务提供对跨域资源共享 (CORS) 的内置支持,使 JavaScript 客户端能够跨域调用 API 应用中托管的 API

文档截图:

【讨论】:

  • 我已经在我的 api 应用程序设置中拥有它,当我使用信号器时出现此错误。我在 angularjs 中遇到错误
  • 请尝试从azure portal设置CORS,看看是否有区别。
  • 我的 api 和本地主机工作正常,我已经在门户中设置了我的 cors,但是当我尝试访问 signalr 时,我得到了 cors 错误。我会检查我是否正确实现了 signalr .
猜你喜欢
  • 2021-04-17
  • 2018-12-24
  • 2019-02-08
  • 2017-10-02
  • 2022-11-28
  • 2021-04-04
  • 2018-07-31
  • 2018-12-16
相关资源
最近更新 更多