Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用WCF启用CORS_Wcf_Ionic Framework - Fatal编程技术网

使用WCF启用CORS

使用WCF启用CORS,wcf,ionic-framework,Wcf,Ionic Framework,我正在尝试使用WCF启用CORS。 但我得到的对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。因此,不允许访问源“”。响应的HTTP状态代码为404 这是我的Global.asax protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Al

我正在尝试使用WCF启用CORS。 但我得到的对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。因此,不允许访问源“”。响应的HTTP状态代码为404

这是我的Global.asax

  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("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }
这是我的web.config


web应用程序在运行时执行跨源HTTP请求 请求具有不同来源(域、协议或 端口)从它自己的

CORS(跨源资源共享)是web浏览器实现SOP(同源策略)的一种方式,该策略规定,只有同一源才能访问服务器的资源

解决方法:使用本机http插件。由于CORS和SOP策略仅由浏览器强制执行,因此在其他http客户端(如本机http调用和邮递员)上不会出现CORS错误。

发生这种情况的原因:

  • 假设登录表单由www.example.com/login返回。现在,来源变成www.example.com
  • 当您单击提交时,浏览器不会立即请求服务器。它将首先通过发送飞行前选项请求来验证服务器是否从该来源接收请求(由于浏览器遵循SOP策略)
  • 如果服务器允许来自此来源的请求,则服务器将发送一些CORS头,包括ACCESS-CONTROL-ALLOW-origin头
更多信息请访问:

  • 如果服务器说是(通过发送有效的CORS标题),浏览器将提交带有原始标题设置为“www.example.com”的表单以及登录参数
  • 在其他情况下,您可能在www.example.com/API/Login server上有一个登录API,如果是IONIC应用程序,您可能会从其他来源发送POST请求
  • 由于来源不同,因此需要在服务器上设置CORS头,以便允许从发出任何请求。但是有一些因素需要记住。与此类似,当您发送带有凭据的请求时,将ACCESS-CONTROL-ALLOW-ORIGIN设置为“*”不起作用
我建议您阅读有关CORS及其政策的文档。它将在将来帮助你。

首先,我怀疑用于创建Restful风格WCF服务的配置有问题。
注意服务部分的
name
属性和端点部分的
contract
属性

<service name="RESTAPI03.test">
          <endpoint address="" behaviorConfiguration="RESTAPI03.testAspNetAjaxBehavior"
              binding="webHttpBinding" bindingConfiguration="TransportSecurity" contract="RESTAPI03.Hotels" />
        </service> 
请参阅我使用
ProtocolMapping
功能的配置。它可以在
HTTP
HTTPS
协议上工作,并且支持您的服务,而不考虑任何服务合同

 <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="httpsbinding">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <protocolMapping>
      <add binding="webHttpBinding" scheme="http"></add>
      <add binding="webHttpBinding" scheme="https" bindingConfiguration="httpsbinding"/>
    </protocolMapping>
  </system.serviceModel>
  • 添加
    global.asax
    文件并修改
    应用程序\u BeginRequest
    事件 随后,将以下配置添加到Web.config文件中

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

    如果问题仍然存在,请随时通知我。

    尝试在客户端代码中添加内容类型并接受标题。另外,您是否正在发送带有凭据的请求?因为在这种情况下,Access Control Allow Origin设置为“*”会抛出一个错误,即如何使用凭据发送请求?我把这样的凭证放错了。const httpOptions={headers:new HttpHeaders({'Content Type':'application/json','Accept':'application/json','Access-Control-Allow-Origin':'*}),with credentials:'true'表示您正在发送cookie、身份验证等凭证。在这种情况下,服务器不应将Acces Control Allow Origin设置为“*”。它应该只允许一个域。稍后,我将发布一个详细的答案和一个绕过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();
                }
            }
    
    if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
                    {
                        Response.End();
                    }
    
       <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>