跨域请求 ASP.NET Web API

3
我有一个与ASP.NET Web API中cors的实现相关的问题。
我之所以向SO求助是因为我已经在各处搜索了很多教程和答案,但似乎没有一个能够适用于我。

安装:

  • Web应用程序(HTML5,JS)运行在XAMPP上(192.168.1.101:8080)
  • 使用IIS Express的API(ASP.net)(与VS2013集成)(192.168.1.101:52126)

如下代码块所示,Web应用程序对API进行ajax调用。

$.ajax({
    data: JSON.stringify(data),
    type:  'POST',
    url:   'http://localhost:52126/api/controller/action',
    dataType: 'json',
    contentType: 'application/json',
    processData: false,
    headers:
    {
        "Authorization": "Basic Base64Encrpytion"
    },
    success: function(data)
    {
        // Handle success
    },
    error: function(data)
    {
        // Handle error
    }
});

这在本地主机上完美运行,包括OPTION预检。但是当我从同一局域网中的其他设备启动Web应用程序时,预检失败,当然,如果预检失败,其余调用也会被取消。


错误:

尝试使用Web应用程序从同一网络中的另一个设备 (192.168.1.100) 查询API时,会生成以下响应:

控制台

OPTIONS http://192.168.1.101:52126/api/controller/action 400 (Bad Request)
OPTIONS http://192.168.1.101:52126/api/controller/action No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.101:8080/' is therefore not allowed access.
XMLHttpRequest cannot load http://192.168.1.101:52126/api/controller/action. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.101:8080/' is therefore not allowed access.

响应头

Connection: close
Content-Length: 334
Content-Type: text/html; charset=us-ascii
Date: Wed, 05 Mar 2014 09:29:46 GMT
Server: Microsoft-HTTPAPI/2.0

在本地测试Web应用和API时,响应头确实包含Access-Control-Allow-Origin“*”头。


我尝试过的解决方案:

将以下代码添加到需要从域外访问的每个类中。

[EnableCors(origins: "*", headers: "*", methods: "*")]

将以下代码添加到 Web.config 文件中。
<httpprotocol>
  <customheaders>
    <add name="Access-Control-Allow-Origin" value="*"></add>
  </customheaders>
</httpprotocol>

将以下代码添加到WebApiConfig文件中。
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

将以下代码添加到applicationhost.config中。
<binding protocol="http" bindingInformation="192.168.1.101:52126:*" />

以下设置已设置为true。
jQuery.support.cors = true

我也尝试过覆盖预检处理程序,但似乎也失败了。


请记住,Chrome不允许您在URL上使用“localhost”,因此无法运行该示例。请使用Firefox或将绑定更改为您的计算机名称。 - balexandre
谢谢您的回复。我已经将localhost更改为机器本身的IP地址,但这样做会弹出错误。当使用localhost时,一切似乎都正常工作。此外,我正在尝试从网络中的另一台设备访问API。 - Jeroen
要在您的网络中公开API而无需部署到IIS,请阅读我的答案以查看旧问题。 - balexandre
1个回答

2

我花了两天时间解决了这个问题。

由于我在公司网络的工作站上开发,没有本地管理员权限。这意味着我必须以管理员模式运行Visual Studio 2013。这样IIS Express就可以在网络上运行,而不仅仅是在本地主机上。

在applicationhost.config中,我做了以下操作:

<binding protocol="http" bindingInformation="*:50157:localhost" />
<binding protocol="http" bindingInformation="*:50157:192.168.1.101" />

显然这种方式失败了,因为它们都被指定到同一个端口。所以我进行了修改:
<binding protocol="http" bindingInformation="*:50157:localhost" />
<binding protocol="http" bindingInformation="*:50158:192.168.1.101" />

TL;DR

  • 确保以管理员模式运行Visual Studio或获取本地管理员权限。
  • 为每个IP使用不同的端口。

谢谢,我所需要做的就是以管理员模式运行我的Visual Studio!再次感谢,我痛苦了将近4个小时,直到找到你的帖子!使用Cors的Signalr会出现XMLHttpRequest无法加载请求的资源上不存在Access-Control-Allow-Origin标头。即使您已经正确地完成了所有操作 - 现在可以微笑了 :) - Jacobus Roos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接