【问题标题】:IIS 10 automatically adds wrong CORS headerIIS 10 自动添加错误的 CORS 标头
【发布时间】:2020-01-15 00:41:51
【问题描述】:

我已经在服务器上安装了 IIS CORS 模块。

根据我得到的 OPTIONS 请求:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE
Access-Control-Allow-Origin: ### the actual good origin ###
Date: Fri, 13 Sep 2019 06:32:11 GMT
Server: Microsoft-IIS/10.0
Vary: Origin
X-Powered-By: ASP.NET

但是在 POST 请求中我得到了

Access-Control-Allow-Origin: http://localhost/
Content-Length: 1216
Content-Type: application/json; charset=utf-8
Date: Fri, 13 Sep 2019 06:32:11 GMT
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET

在网络配置中我有

  <cors enabled="true">
  <add origin="### the actual good origin ###" allowCredentials="true" >

    <allowHeaders allowAllRequestedHeaders="true" />
    <allowMethods >
        <add method="GET" />
        <add method="HEAD" />
        <add method="POST" />
        <add method="PUT" /> 
        <add method="DELETE" />         
    </allowMethods>
  </add>
</cors>

我尝试调用的 WebService 是 WCF 网络服务。

如何禁用 POST 请求中的“localhost”标头? 我没有在 web.config 和 IIS 本身中设置任何静态标头

【问题讨论】:

  • 您可以尝试使用 iis HTTP 响应标头添加标头。 Access-Control-Allow-Origin 和值 null。有关更多详细信息,您可以参考link

标签: wcf iis cors


【解决方案1】:

建议您尝试将Global.asax文件添加到WCF项目中,用于解决CORS问题。

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With,Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }

或者,我们也可以在 webconfig 文件中进行配置。

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
            {
                Response.End();
            }

        }

网络配置。

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <directoryBrowse enabled="true" />
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="content-type" />
        <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

如果问题仍然存在,请随时告诉我。

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 2017-05-23
    • 2015-05-02
    • 2013-08-09
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 2018-02-13
    相关资源
    最近更新 更多