【问题标题】:How to allow CORS request for particular domains in WCF如何在 WCF 中允许对特定域的 CORS 请求
【发布时间】:2019-10-03 12:47:47
【问题描述】:

1.) 无法从客户端向 WCF 服务发出 CORS 请求。 2.) 如何配置 WCF 以允许来自特定域的 CORS 请求?

我尝试使用一些配置设置,例如 crossDomainScriptAccessEnabled 自定义标题 在 web.config 中,但仍然出现 CORS 错误。

帮助我根据域名或 IP 自定义对特定请求的访问。

 <system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
  </webHttpBinding>

</bindings>
<behaviors>

 <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>

  <endpointBehaviors>
    <behavior name="EndPointBehaviour">
      <webHttp/>
      <enableWebScript />
    </behavior>
  </endpointBehaviors>


</behaviors>

<services>
  <service name="MyNameSpace.MyService" behaviorConfiguration="ServiceBehaviour">
   <endpoint 
      address="" behaviorConfiguration="EndPointBehaviour"
      binding="webHttpBinding"
      contract="MyNameSpace.IMyService" bindingConfiguration="crossDomain" />    
  </service>
</services>

<protocolMapping>
  <add binding="webHttpBinding" scheme="http" bindingConfiguration="crossDomain" />
  <add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

 <system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
  -->
<directoryBrowse enabled="true"/>

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*"/>
    <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
    <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
    <add name="Access-Control-Max-Age" value="1728000" />
  </customHeaders>
</httpProtocol>

【问题讨论】:

    标签: c# wcf cors


    【解决方案1】:

    尝试在 global.asax.cs 文件中使用此代码:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");
    
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }
    

    【讨论】:

    • 如何允许多个来源
    • @chandrarv 只允许添加一个域。您必须创建一个“白名单”并检查是否允许 URL。您可以使用Request.UrlReferrer.GetLeftPart(UriPartial.Authority)检索域
    • 我可以给每个服务客户一个唯一的密钥并根据该密钥验证 CORS 吗?
    • @chandrarv 不,您需要将域添加到标题中。您可以将域列表存储在 web.config 文件中,并创建一个帮助程序来验证在Request.UrlReferrer.GetLeftPart(UriPartial.Authority) 中收到的域是否属于允许的域。如果是,将其添加到标题中
    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 2014-01-06
    • 2015-10-11
    • 2013-06-05
    • 2023-03-10
    • 2018-07-28
    • 1970-01-01
    • 2015-05-22
    相关资源
    最近更新 更多