You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在本地部署的Microsoft Dynamics CRM 365上启用CORS?

本地非联网Microsoft Dynamics CRM 365的CORS配置方案

可以在本地CRM 365上配置CORS,但不能使用通配符*,必须指定具体的SPA源地址——因为带身份验证的跨域请求,浏览器不允许使用通配符的CORS配置。以下是具体实现方案:

1. 通过CRM PowerShell Cmdlet配置CORS

以管理员身份打开PowerShell,执行以下步骤:

  • 加载CRM PowerShell模块:
    Add-PSSnapin Microsoft.Crm.PowerShell
    
  • 获取当前CORS配置并修改:
    # 获取现有CORS设置
    $corsSettings = Get-CrmSetting -SettingType CorsSettings
    
    # 设置允许的源(替换为你的SPA实际地址,多个源用逗号分隔)
    $corsSettings.AllowedOrigins = @("https://your-spa-domain.com", "https://localhost:3000")
    
    # 设置允许的请求头(按需添加,比如身份验证头、内容类型头)
    $corsSettings.AllowedHeaders = @("Content-Type", "Authorization")
    
    # 设置允许的HTTP方法
    $corsSettings.AllowedMethods = @("GET", "POST", "PUT", "DELETE")
    
    # 保存配置
    Set-CrmSetting -Setting $corsSettings
    
  • 重启CRM对应的IIS站点,确保配置生效。

2. 前端请求注意事项

在你的JavaScript SPA中发送请求时,必须开启凭证携带:

  • 使用fetch时:
    fetch('https://your-crm-domain/api/data/v9.1/accounts', {
      method: 'GET',
      credentials: 'include', // 携带身份凭证(Cookie/AD信息)
      headers: {
        'Content-Type': 'application/json'
      }
    })
    
  • 使用XMLHttpRequest时:
    const xhr = new XMLHttpRequest();
    xhr.open('GET', 'https://your-crm-domain/api/data/v9.1/accounts');
    xhr.withCredentials = true; // 开启凭证携带
    xhr.send();
    

3. 验证配置

发送跨域请求后,检查响应头是否包含Access-Control-Allow-Origin,其值应为你指定的SPA源地址,而非通配符。

内容的提问来源于stack exchange,提问作者TomS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:45:42