jquery - CORS 启用 WCF REST 和 Kendo

标签 jquery cors wcf-rest kendo-dataviz

我正在 wcf 休息服务工作。我需要允许 cors 支持跨域访问。我阅读了文章并得到了两种方法。

第一种方法是在 global.asax 的 application_beginrequest 事件中添加 http header 。这对我来说效果很好。我使用 jquery 使用剑道图表绑定(bind)对此进行了测试。图表在 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的属性,所以我遵循了这个link 。我复制并运行该服务,结果成功。但当我在端点中启用此行为后,客户端没有在 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>

你能帮我解决我错过的地方吗?

最佳答案

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

关于jquery - CORS 启用 WCF REST 和 Kendo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21602415/

相关文章:

javascript - 单击时如何在表格行内切换 div

javascript - 如何查找字符串中的数字模式

angular - 如何配置.net core以将身份验证cookie发送到不同的域

ajax - 在 Tomcat 中禁用 OPTIONS 请求的身份验证

jquery - 具有最小和最大约束的可缩放背景图像

android - 如何在 focus() 上以编程方式隐藏 jquery mobile 中的键盘

.net - ASP.NET 将 CORS header 添加到 ashx 文件处理程序

authentication - 使用 OAUTH 2.0 调用 WCF Restful 方法

windows - 如何将当前 Windows 凭据从使用 JS 制作的 Windows 8 native 应用程序传递到 WCF REST 服务?

WCF:更改 ClientCredentials 会产生 "Manual addressing is enabled on this factory, so all messages sent must be pre-addressed."