Asp.net mvc Chrome 27中的信号器和跨域请求问题

Asp.net mvc Chrome 27中的信号器和跨域请求问题,asp.net-mvc,iis-7,cross-domain,signalr,Asp.net Mvc,Iis 7,Cross Domain,Signalr,编辑:这显然只是Chrome中的一个问题,它在FF和IE中运行良好 Chrome版本:27.0.1453.116 我在localhost:13371上有一个简单的JS/HTML站点,我正在尝试访问localhost:13371上的信号器中心 无论何时提出请求,我都会收到以下错误: XMLHttpRequest无法加载http://localhost:13370/signalr/hubs/negotiate?_=1372338722032。来源http://localhost:13371是访问控制

编辑:这显然只是Chrome中的一个问题,它在FF和IE中运行良好

Chrome版本:27.0.1453.116

我在localhost:13371上有一个简单的JS/HTML站点,我正在尝试访问localhost:13371上的信号器中心

无论何时提出请求,我都会收到以下错误:

XMLHttpRequest无法加载
http://localhost:13370/signalr/hubs/negotiate?_=1372338722032
。来源<代码>http://localhost:13371是访问控制允许原点不允许的

我已经尝试过的:
  • 启用
    应用程序启动时信号器上的跨域:

    RouteTable.Routes.MapHubs(new HubConfiguration { EnableCrossDomain = true });
    
  • 在Signal server的Web.Config中启用跨域:

    <system.webServer>
       <httpProtocol>
         <customHeaders>
           <clear />
           <add name="Access-Control-Allow-Origin" value="*" />
           <add name="Access-Control-Allow-Methods" value="*" />
           <add name="Access-Control-Allow-Headers" value="*" />
         </customHeaders>
       </httpProtocol>
    </system.webServer>
    
  • 在jQuery中启用CORS:

    $.support.cors = true;
    
  • 应用程序中手动设置响应头\u BeginRequest

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            //These headers are handling the "pre-flight" OPTIONS call sent by the browser
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "*");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }
    
上述任何一项或以上任何组合都不起作用。 其他信息:

  • 这两个站点都在Windows 7上的IIS中运行
  • 信号器站点在端口13370上运行,作为.NET 4.0下MVC 4站点的一部分
  • JS/HTML站点是端口13371上的一个简单Web服务器,根本没有托管代码
  • SignalR站点肯定会响应浏览器对相关资源的直接请求
  • 信号机版本为1.1.2
编辑:我现在找到了两种解决方案。。。 方法1。扔掉你可能不需要的垃圾: 正如我在这篇文章中发现的,我在上面列出的“我尝试过的事情”中添加的几乎所有内容都是不必要的。修复的步骤:

  • 删除上面列出的我尝试过的所有内容。这意味着在Web.Config或Global.asax等其他地方根本没有指定自定义头,也没有自定义jquery设置,等等

  • 。。除了
    RouteTable.Routes.MapHubs(新的HubConfiguration{EnableCrossDomain=true})应用程序启动中的code>

  • 此外,您还需要设置
    $.connection.hub.url=http://localhost:13370/signalr/hubs';

  • 。。。就这样这可能是最好的解决方案,也是我最终使用的解决方案。

    方法2。如果您在Chrome中仍然存在问题,请使用jsonp: 如果你在Chrome中仍然有这样的问题,你可以使用jsonp让协商脚本正确下载。。。将以下内容添加到my JavaScript hub start解决了此问题:

    //detect chrome
    var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    
    //set the connection url.
    $.connection.hub.url = 'http://localhost:13370/signalr/hubs';
    
    //use jsonp if chrome
    $.connection.hub.start({
        jsonp: isChrome
    });
    

    可能有更好的方法来测试浏览器功能并相应地设置jsonp。。。看着用户代理,感觉像地狱一样肮脏。。。但这在过渡期间解决了我的问题。我希望这对其他人有帮助。

    这在文档中,但永远不要这样做:


    $.support.cors=true

    对我有效的方法如下

    protected void Application_Start(object sender, EventArgs e)
        {
            var config = new HubConfiguration
            {
                EnableCrossDomain = true
            };
            RouteTable.Routes.MapHubs(config);
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (this.Context.Request.Path.Contains("signalr/"))
            {
                this.Context.Response.AddHeader("Access-Control-Allow-Headers", "accept,origin,authorization,content-type");
            }
        }
    

    这对我有用。将此方法放在Global.asax.cs上

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        Context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
        var referrer = Request.UrlReferrer;
        if (Context.Request.Path.Contains("signalr/") && referrer != null)
        {
            Context.Response.AppendHeader("Access-Control-Allow-Origin", referrer.Scheme + "://" + referrer.Authority);
        }
    }
    

    我在MVC6 SignarR服务器上也遇到了同样的问题。
    刚刚在软件包管理器控制台中安装了
    Install Package Microsoft.Owin.Cors
    ,并在Startup.cs中添加了
    app.UseCors(CorsOptions.AllowAll)

    你试过阅读吗?我做了与上面方法1相同的事情,虽然我没有收到任何错误,但我的客户拒绝连接到我的信号集线器。它进入了尝试与中心协商的步骤,但无法建立连接,因为结果对象的协议版本(从Ajax尝试建立连接时开始)与连接的clientProtocol属性不同。前者为1.4,后者为1.5。因此,连接被拒绝。这篇文章中的任何建议对我都不起作用。还有其他想法吗?
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        Context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
        var referrer = Request.UrlReferrer;
        if (Context.Request.Path.Contains("signalr/") && referrer != null)
        {
            Context.Response.AppendHeader("Access-Control-Allow-Origin", referrer.Scheme + "://" + referrer.Authority);
        }
    }