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. Jquery禁用/启用按钮与文本框代码
  2. 推荐web 前端代码的编辑分享平台——RunJS
  3. 想要改进我的jQuery代码,做我想要的,太丑了,IMO
  4. JQuery的一些基本功能代码(CH1&2)
  5. 如何让代码等待好/取消按钮选择?
  6. 如何在angularjs代码中单元测试jquery元素
  7. jquery常用代码片段
  8. 在Rails应用程序中结合Scriptaculous和JQuery
  9. Webhoster插入了一个javascript,它会阻止我的代码如何删除它?

随机推荐

  1. Android Fresco属性大全,中文说明
  2. Android 蓝牙4.0代码解析
  3. 8天快速掌握Android开发
  4. Android剖析和运行机制
  5. Android(安卓)Shell 常用命令
  6. 实现底部导航栏中间凸起
  7. Android最新框架
  8. 关于android:layout_x 与 android:layout
  9. Android中style的使用
  10. Android启动流程分析(二) init进程的启动