在WCF REST服务CORS支持 [英] CORS Support within WCF REST Services

查看:1360
本文介绍了在WCF REST服务CORS支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Windows服务托管一个WCF REST服务,我想送的访问控制 - 允许 - 产地HTTP头(定义为的 CORS )每回应。

I have a WCF REST service hosted within a Windows service and I would like to send the Access-Control-Allow-Origin HTTP header (defined as part of CORS) with every response.

我尝试的解决办法是有一些像在<一以下href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector.aspx"相对=nofollow> IDispatchMessageInspector 实现:

My attempted solution was to have something like the following within an IDispatchMessageInspector implementation:

public void BeforeSendReply(ref Message reply, object correlationState)
{
    var httpResponse = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
    if (httpResponse != null)
    {
        // test of CORS
        httpResponse.Headers["Access-Control-Allow-Origin"] = "*";
    }
}

通常情况下这会工作,但不幸的是我的服务也使用 HTTP基本授权,这意味着当收到请求时没有授权头,WCF会自动发送一个401响应请求凭据。不幸的是WCF不初始交换期间打电话给我IDispatchMessageInspector,所以访问控制 - 允许 - 产地标头不添加到初始交换。

Normally this would work, but unfortunately my service also uses HTTP basic authorization, which means that when a request comes in without the Authorization header, WCF automatically sends a 401 response asking for credentials. Unfortunately WCF does not call my IDispatchMessageInspector during this initial exchange, so Access-Control-Allow-Origin header is not added to the initial exchange.

当我尝试从浏览器中调用该服务时出现问题。 CORS指定跨域请求,应仅在原始域的访问控制 - 允许 - 原产地响应头(*匹配所有域)中列出的域相匹配被允许。不幸的是,当浏览器看到没有访问控制 - 允许 - 产地标头的最初401响应,它prevents访问(根据的同源策略)。

The problem occurs when I try to call the service from a browser. CORS specifies that cross-origin requests should only be allowed if the origin domain matches the domain listed in the Access-Control-Allow-Origin response header (* matches all domains). Unfortunately when the browser sees the initial 401 response without the Access-Control-Allow-Origin header, it prevents access (according to the same origin policy).

有没有办法添加一个头的最初401响应由WCF自动发送?

Is there any way add a header to the initial 401 response sent automatically by WCF?

推荐答案

这家伙救了我的一天。

<一个href="http://blogs.microsoft.co.il/blogs/idof/archive/2011/07.aspx">http://blogs.microsoft.co.il/blogs/idof/archive/2011/07.aspx

我准备把他的一些笔记这里,以防万一该网页死亡的一天。 (我讨厌发现你的答案就在这里链接,然后链接已经死了。)

I am going to place some of his notes here, just in case that web page dies some day. (I hate finding "Your answer is right HERE" links, and then the link is dead.)

<behaviors> 
  <endpointBehaviors> 
    <behavior name="webSupport"> 
      <webHttp /> 
      <CorsSupport /> 
    </behavior> 
  </endpointBehaviors> 
</behaviors> 
<extensions> 
  <behaviorExtensions> 
    <add name="CorsSupport" type="WebHttpCors.CorsSupportBehaviorElement, WebHttpCors, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> 
  </behaviorExtensions> 
</extensions> 
<services> 
  <service name="Service.JSonService"> 
    <endpoint address="http://localhost:8080" behaviorConfiguration="webSupport" binding="webHttpBinding" contract="Service.IJSonService" /> 
  </service> 
</services>

现在,你必须要找到自己的下载库被称为WebHttpCors.dll。

Now, you have to find his downloadable library called "WebHttpCors.dll".

但有足够的有(上图),以帮助你谷歌/兵用自己的方式的决议。

But there is enough there (above) to help you google/bing your way to a resolution.

这是扔我一个循环(在我的情况)的部分是,IE浏览器是工作,但Firefox是行不通的。

The part that was throwing me for a loop (in my scenario) is that IE was working, but Firefox was not working.

我的原始页面:

http://localhost:53692/test/WCFCallTestViaJQ14.htm

所以我的服务是:

So my service is at:

http://localhost:8002/MyWCFService/MyWCFMethodByWebGet?state=NC&city=Raleigh

所以我只好本地主机&LT;&LT; - >>本地主机的流量

So I had localhost <<-->> localhost traffic.

的但端口不同。 (53692和8002)**

But the ports were different. (53692 and 8002) **

IE浏览器是好的吧。火狐是不正常吧。

IE was ok with it. Firefox was not ok with it.

然后,你得记住不同的处理(内JQUERY)的。发送()请求。

Then you gotta remember that handle their .Send() requests differently (inside JQUERY).

这一切都有道理了。

//JavaScript snipplet

if (window.XMLHttpRequest) {

    returnObject = new XMLHttpRequest();

} else if (window.ActiveXObject) {

    returnObject = new ActiveXObject("Microsoft.XMLHTTP");

} else {

msg = "Your browser doesn't support AJAX!";

}

下面是一些关键的单词,短语,我一直在谷歌上搜索/暴食是最终导致我的地方。

Here are some key words, phrases that I've been googling/binging that finally led me somewhere.

    Result: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.statusText]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://localhost:53692/test/WCFCallTestViaJQ14.htm :: HandleJQueryError :: line 326" data: no]


XMLHttpRequest Send "NS_ERROR_FAILURE"

JQuery Ajax WCF Self Hosted CORS JSON

这篇关于在WCF REST服务CORS支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆