【问题标题】:How to enable CORS in Dynamics CRM?如何在 Dynamics CRM 中启用 CORS?
【发布时间】:2014-11-11 20:22:04
【问题描述】:

请注意,该问题专门针对 Dynamics CRM。我希望有一种专有功能可以扩展或取代常见的 Web 开发。因此,这个问题不是重复的,尽管一旦看到“CORS”和这个“又一个鸭子在询问 CORS...”(意为错字),它可能看起来如此。

我正在尝试从 CRM 拨打外部消息,当然,我遇到了 CORS 问题。现在,我对调用定向到的服务器端几乎没有控制权,所以我想知道是否有可能从客户端以某种方式解决它。

最佳解决方案是服务器开发人员允许来自不同域的调用,但存在他们不会这样做的风险。那么,除了编写一个自定义服务层以向来自 CRM 的调用与第三方服务器对话时,我还有什么选择?

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

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

【问题讨论】:

  • 您尝试过使用 JSONP 吗?我们有一个基于 javascript 的 CRM Web 资源,它调用外部 WebService,它对我们有用。 api.jquery.com/jquery.getjson
  • 它像鸭子一样令人困惑(故意错字)但如果这是唯一的方法......

标签: javascript ajax cors dynamics-crm dynamics-crm-2013


【解决方案1】:

这是一个老问题,但我在 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并添加以下规则

<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>

【讨论】:

  • 我将 +1 用于持久性,但这不是解决问题的方法。遗憾的是,它需要在我们无法访问 web.config 的在线发行版中工作。不过,我已经不在那个公司了,也不再是我的问题了,呵呵。另外,我不认为它可以按照我们希望的方式解决。不过,正如我所提到的,+1。
猜你喜欢
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多