2

我在 wcf 休息服务部门工作。我需要允许 cors supprt 进行跨域访问。我阅读了文章并获得了2种方法。

第一种方法是在 global.asax 中的 application_beginrequest 事件中添加 http 标头。这对我来说很好。我使用 jquery 使用剑道图表绑定对此进行了测试。图表在 IE、Chrome 和 Firefox 中填充。在 global.asax 中启用 cors 的工作代码是

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            //These headers are handling the "pre-flight" OPTIONS call sent by the browser
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }

但我需要配置 cors enabled 属性,所以我点击了这个链接。我复制并运行了成功的服务。但是在我在端点中启用此行为后,客户端没有在 Chrome 和 Firefox 中显示图表。所以没有启用跨域。我对吗?我想念这里的地方。

我的新服务课程是;

public class CorsEnabledBehavior : BehaviorExtensionElement, IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
        var requiredHeaders = new Dictionary<string, string>();

        requiredHeaders.Add("Access-Control-Allow-Origin", "*");
        requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS");
        requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");

        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CorsEnabledMessageInspector(requiredHeaders));
    }

    public void Validate(ServiceEndpoint endpoint)
    {

    }

    public override Type BehaviorType
    {
        get { return typeof(CorsEnabledBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new CorsEnabledBehavior();
    }

}

public class CorsEnabledMessageInspector : IDispatchMessageInspector
{
    Dictionary<string, string> requiredHeaders;
    public CorsEnabledMessageInspector(Dictionary<string, string> headers)
    {
        requiredHeaders = headers ?? new Dictionary<string, string>();
    }

    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
    {
        return null;
    }

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
        foreach (var item in requiredHeaders)
        {
            httpHeader.Headers.Add(item.Key, item.Value);
        }
    }
}

我的网络配置是

<extensions>
  <behaviorExtensions>
    <add name="corsEnabledBehaviour" type="LAMI.Service.Utilities.CorsEnabledBehavior, LAMI.Service.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>

<behaviors>
  <endpointBehaviors>
    <behavior name="endBehaviour1">
      <webHttp helpEnabled="true" />
      <corsEnabledBehaviour />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="serviceBehaviour1">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="webHttpConfiguration"  >
    </binding>
  </webHttpBinding>
</bindings>

<services>
  <service behaviorConfiguration="serviceBehaviour1" name="LAMI.Service.Service1">
    <endpoint address="" behaviorConfiguration="endBehaviour1" binding="webHttpBinding"
      bindingConfiguration="webHttpConfiguration" contract="LAMI.Service.Contract.IService1" />
    <host>
      <baseAddresses>
        <add baseAddress="http://ltms0/ServiceApp/Service1/" />
      </baseAddresses>
    </host>
  </service>
</services>

你能帮我我想念的地方吗?

4

1 回答 1

2

我完成了这个。我使用 global.asax 启用了 cors 并成功运行。问题是iecors.js。这无法在 IE 8 和 9 中启用 cors。所有其他浏览器工作正常

于 2014-02-13T04:07:01.367 回答