I have a question regarding the implementation of cors in a ASP.NET Web API. The reason I am turning to SO is that I've tried searching everywhere, and found a lot of tutorials and answers, but none of those seem to work for me.
我有一个关于在ASP中实现cors的问题。净Web API。我之所以求助于SO,是因为我尝试过到处搜索,找到了很多教程和答案,但似乎没有一个对我有用。
Setup:
- Web Application (HTML5, JS) running on XAMPP (192.168.1.101:8080)
- 在XAMPP上运行的Web应用程序(HTML5, JS) (192.168.1.101:8080)
- API (ASP.net) running on IIS Express (Integrated with VS2013) (192.168.1.101:52126)
- 在IIS Express上运行的API (ASP.net)(与VS2013集成)(192.168.1.101:52126)
The web application makes ajax calls to the API as shown in the code block below.
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
}
});
This all works perfectly in localhost including OPTION preflight. But when I start the web application from a other device in the same local network, the preflight fails and ofcourse if the preflight fails the rest of the calls get canceled as well.
这在localhost中都能很好地工作,包括选项preflight。但是,当我从同一个本地网络中的其他设备启动web应用程序时,它会失败,当然,如果preflight失败,其他调用也会被取消。
Error:
When trying to query the API from another device (192.168.1.100) in the same network using the web application the following responses are generated:
当尝试使用web应用程序从同一网络中的另一个设备(192.168.1.100)查询API时,将生成以下响应:
Console
控制台
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.
Response Header
响应头
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
The response header does contain the Access-Control-Allow-Origin "*" header when testing the web application and API on localhost.
在本地主机上测试web应用程序和API时,响应头确实包含访问控制允许的“*”头。
Solutions I have tried:
Adding the code below to each class that needs to be reached from outside the domain.
将下面的代码添加到需要从域外部访问的每个类中。
[EnableCors(origins: "*", headers: "*", methods: "*")]
Adding the code below to the Web.config file.
将下面的代码添加到Web中。配置文件。
<httpprotocol>
<customheaders>
<add name="Access-Control-Allow-Origin" value="*"></add>
</customheaders>
</httpprotocol>
Adding the code below to the WebApiConfig file.
将下面的代码添加到WebApiConfig文件中。
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
Adding the code below to the applicationhost.config
将下面的代码添加到application .config中
<binding protocol="http" bindingInformation="192.168.1.101:52126:*" />
The following setting is set to true
下面的设置设置为true。
jQuery.support.cors = true
I have also tried to override the preflight handler, but that seemed to fail as well.
我还试图重写飞行前处理程序,但似乎也失败了。
1 个解决方案
#1
2
Wasting two days on this problem, i think i've solved it.
在这个问题上浪费了两天时间,我想我已经解决了。
Since i am developing on a workstation in a corporate network i don't have local administrator rights. Which means i have to run Visual Studio 2013 in Administrator Mode. That way IIS Express can run on the network instead of only localhost.
由于我是在公司网络的一个工作站上开发的,所以我没有本地管理员的权限。这意味着我必须在管理员模式下运行Visual Studio 2013。这样,IIS Express就可以在网络上运行,而不仅仅是本地主机。
In the applicationhost.config i did the following:
applicationhost。配置我做了以下:
<binding protocol="http" bindingInformation="*:50157:localhost" />
<binding protocol="http" bindingInformation="*:50157:192.168.1.101" />
Which obviously fails because they are both assigned to the same port. So i changed that to:
这显然是失败的,因为它们都被分配到同一个端口。所以我把它改成:
<binding protocol="http" bindingInformation="*:50157:localhost" />
<binding protocol="http" bindingInformation="*:50158:192.168.1.101" />
TL;DR
博士TL;
- Make sure you run Visual Studio in adminstrator mode or just obtain local administrator rights.
- 确保您在adminstrator模式下运行Visual Studio,或者仅仅获得本地管理员权限。
- Use a different port for each ip.
- 对每个ip使用不同的端口。
#1
2
Wasting two days on this problem, i think i've solved it.
在这个问题上浪费了两天时间,我想我已经解决了。
Since i am developing on a workstation in a corporate network i don't have local administrator rights. Which means i have to run Visual Studio 2013 in Administrator Mode. That way IIS Express can run on the network instead of only localhost.
由于我是在公司网络的一个工作站上开发的,所以我没有本地管理员的权限。这意味着我必须在管理员模式下运行Visual Studio 2013。这样,IIS Express就可以在网络上运行,而不仅仅是本地主机。
In the applicationhost.config i did the following:
applicationhost。配置我做了以下:
<binding protocol="http" bindingInformation="*:50157:localhost" />
<binding protocol="http" bindingInformation="*:50157:192.168.1.101" />
Which obviously fails because they are both assigned to the same port. So i changed that to:
这显然是失败的,因为它们都被分配到同一个端口。所以我把它改成:
<binding protocol="http" bindingInformation="*:50157:localhost" />
<binding protocol="http" bindingInformation="*:50158:192.168.1.101" />
TL;DR
博士TL;
- Make sure you run Visual Studio in adminstrator mode or just obtain local administrator rights.
- 确保您在adminstrator模式下运行Visual Studio,或者仅仅获得本地管理员权限。
- Use a different port for each ip.
- 对每个ip使用不同的端口。