配置IIS Express 8以启用CORS [英] Configure IIS Express 8 to enable CORS

查看:702
本文介绍了配置IIS Express 8以启用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的WCF服务将被客户端在野外使用,因此他们需要处理跨源请求。我有一个问题,使我的开发服务器接受这样的请求。下面是场景:

I'm writing WCF services that will be used by clients out in the wild so they need to handle cross-origin requests. I have a problem with enabling my development server to accept such requests. Here is the scenario:


  • 我在Visual Studio 2012的一个实例中运行WCF项目,使用IIS Express 8作为服务器

  • 我在Visual Studio 2012的另一个实例中运行客户端项目,也使用IIS Express 8作为服务器。

当我在IE中运行客户端项目没有问题,因为IE不发送预检OPTIONS请求。当我在Chrome中运行它时,preflight OPTIONS请求返回405方法不允许,Chrome放弃该服务。以前的Chrome版本只会忽略错误,并继续实际的POST请求(或Get,无论什么...),但后来的版本似乎更挑剔。

When I run the client project in IE there is no problem because IE does not send the preflight OPTIONS request. When I run it in Chrome however the preflight OPTIONS request returns a 405 Method Not Allowed and Chrome gives up on the service. Previous versions of Chrome would just ignore the error and continue with the actual POST request (or Get, whatever...) but later versions appear to be pickier.

ve也在一个部署的WCF项目中运行,并通过将OPTIONSVerbHandler移动到IIS中的处理程序映射列表的顶部来解决它。

I've also run into this with a deployed WCF project and solved it by moving the OPTIONSVerbHandler to the top of the Handler Mappings list in IIS.

我应该指出, m使用最慷慨的web.config设置我可以想到尝试允许CORS。例如我在WCF项目的配置中有这个:

I should point out that I'm using the most generous web.config settings I can think of to try to allow CORS. For instance I have this in the WCF project's configuration:

<httpProtocol>
  <customHeaders>
    <remove name="X-Powered-By" />
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="*" />
    <add name="Access-Control-Allow-Methods" value="*" />
    <add name="X-Powered-By" value="*" />
  </customHeaders>
</httpProtocol>

无论如何,任何从WCF项目运行的客户端跨源请求都会失败,405错误。

Regardless, any client cross-origin requests to the WCF project running from code fail with the 405 error.

任何帮助设置WCF项目本身或IIS Express 8以启用CORS?

Any help setting up either the WCF project itself or IIS Express 8 to enable CORS?

谢谢! / p>

Thanks!

推荐答案

答案是,使WCF接受CORS预检邮件所需的配置与IIS服务器无关;而是WCF项目本身需要配置为处理带有OPTIONS动词的HTTP请求。

The answer is that the configuration needed to enable WCF to accept CORS preflight messages has nothing to do with the IIS server; rather the WCF project itself needs to be configured to handle the HTTP request with OPTIONS verb.

长的故事:这是真的很难。 WCF是所有交易的杰克,当谈到端点,所以设置它做一些非常具体的一个(HTTP)是不可取的,虽然可以做到。真正的解决方案是使用Web API,它是HTTP的主人,并且可以设置为非常简单地做CORS。

Long story short: doing this is REALLY HARD. WCF is a jack of all trades when it comes to endpoints so setting it up to do something very specific with one (HTTP) is not advisable, although it can be done. The real solution is to use Web API, which is a master of HTTP and can be set up to do CORS very simply.

这篇关于配置IIS Express 8以启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆