【问题标题】:LocalHost CORS failing in Preflight for Azure APIMLocalHost CORS 在 Azure APIM 的预检中失败
【发布时间】:2021-11-06 03:35:25
【问题描述】:

尝试从 localhost 与我的 Azure APIM 端点通信时,我遇到了 CORS 预检失败。我已经修改了 APIM 和 .Net Core API 本身的政策,但我仍然没有运气。以下是我的政策在 APIM 中“所有操作”部分的外观。如果有帮助的话,前端是一个角度应用程序。在 GET 上,我还在标题中附加了 ocp-apim-subscription-key。

         <cors>
            <allowed-origins>
                <origin>*</origin>
            </allowed-origins>
            <allowed-methods preflight-result-max-age="300">
                <method>*</method>
            </allowed-methods>
            <allowed-headers>
                <header>*</header>
            </allowed-headers>
            <expose-headers>
                <header>*</header>
            </expose-headers>
        </cors>

【问题讨论】:

    标签: angular azure localhost apim


    【解决方案1】:
    • 您应该在 Azure app servicesAPIM 中都配置 CORS。
    • 请检查您的 web 应用中的跨域设置。
    • 配置自定义标头:Seckey如果您的 CORS 策略中有。
    • 将以下内容添加到您的 web.config。
    • 您可能需要根据您针对 API 发出的请求添加其他标头,例如 Access-Control-Allow-Methods 和 Access-Contorl-Allow-Headers。
      <system.webServer>  
           <httpProtocol> 
            <customHeaders>  
             <add  name="Access-Control-Allow-Origin"  value="*"  />            
            </customHeaders> 
           </httpProtocol> 
            </system.webServer>
    

    **更多详情请参考以下文件**

    Failed to call APIs from web page behind Azure APIM due to CORS issues

    Getting Access Control Allow Origin Error While Accessing Web Api hosted in AZURE RRS feedCORS Preflight request

    【讨论】:

    • 这针对 localhost,而不是应用服务,所以在应用服务中设置 cors 不会解决这个问题吗?并且标题
      *
      中的通配符 * 不应该允许飞行前的所有标题吗?我的预检有 Access-Control-Request_headers、Accesss-Control-Request-Method、Origin、Sec-Feth-Mode 和 Accept。
    • API 的各个应用服务在 cors 部分也有 *
    【解决方案2】:

    在与我公司的 Devops 团队和 IT 管理人员合作后,我得以解决这个问题。显然,我公司的计算机需要我们的 Windows HOST 文件中来自天蓝色的服务器 APIMs IP 的工作域。一旦我添加了它,我就没有任何问题,并且这些政策能够发挥作用。

    【讨论】:

      猜你喜欢
      • 2020-01-02
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2017-01-22
      • 2019-02-18
      • 2015-08-08
      • 2016-11-25
      • 1970-01-01
      相关资源
      最近更新 更多