如何在Dynamics CRM中启用CORS



请注意,此问题专门针对Dynamics CRM。我希望有一个专有的功能来扩展或取代常见的web开发。因此,这个问题并不是重复的,尽管一旦人们看到"CORS"和这个"又一只鸭子问CORS……"(有意拼写错误),它可能看起来是重复的

我正试图打电话给CRM的外部消息,当然,我遇到了CORS问题。现在,我对调用指向的服务器端几乎没有控制权,所以我想知道是否有可能从客户端解决这个问题。

最佳解决方案是,服务器开发人员允许来自不同域的调用,但存在这样做的风险。那么,除了编写一个自定义服务层,为来自CRM的呼叫打开CORS,并且与第三方服务器对话之外,我还有什么选择呢?

nag如下(当然,从URL行来看,调用执行得非常好)。

阻止跨来源请求:同源策略不允许读取上的远程资源https://yaba.daba.doo/list?p1=[]&p2=0&amp_=1415714629958。这可以通过将资源移动到同一域或启用CORS来解决。

这是一个老问题,但我在CRM 2011(和IIS 7.0)编辑web.config时解决了这个问题。

我已将CORS标头添加到IIS响应中。

添加基本标题

<system.webServer>
  <httpProtocol>
     <customHeaders>
        <add name="Access-Control-Allow-Headers" value="Origin, X-HTTP-Method, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,PUT,DELETE" />
        <add name="Access-Control-Allow-Credentials" value="true" />
     </customHeaders>
   </httpProtocol>[..]

添加规则

安装Url重写并在下面添加规则

<rewrite>
    <rules>
        [..]
        <!-- This rule allow the prefligh request to be authenticated, otherwise raise the 401 error, required ONLY if you use POST, for GET only is not necessary -->
        <rule name="CORS Preflight Anonymous Authentication" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{REQUEST_METHOD}" pattern="^OPTIONS$" />
            </conditions>
            <action type="CustomResponse" statusCode="200" statusReason="Preflight" statusDescription="Preflight" />
        </rule>
        [..]
    </rules>
    <outboundRules>
        <clear />                
        <rule name="AddCORSHeaders">
            <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                <!-- This add the Access-Control-Allow-Origin if the pattern match the request url -->
                <add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+.)?domain1.lan|(.+.)?mydomain.com|(.+.)?otherdomain.com))" />
            </conditions>
            <action type="Rewrite" value="{C:0}" />
        </rule>           
    </outboundRules>
</rewrite>

最新更新