2012-09-19 24 views
2

我有一個WCF服務定義爲:WCF服務和AspNetCompatibilityEnabled = 「真」,導致請求錯誤

[ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
public class Service 
{ 
    [OperationContract] 
    [WebGet(ResponseFormat = WebMessageFormat.Json)] 
    public string HelloWorld() 
    { 
     return "Hello World"; 
    } 
} 

我的web.config文件:

<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
      <behavior name="webHttpBehavior"> 
       <webHttp /> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <bindings> 
     <webHttpBinding> 
     <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"/> 
     </webHttpBinding> 
    </bindings> 
    <services> 
     <service name="Service"> 
     <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="Service" behaviorConfiguration="webHttpBehavior"/> 
     </service> 
    </services> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/> 
    </system.serviceModel> 
</configuration> 

我希望能夠訪問ASP .Net會話變量在我的WCF服務中,並且我希望WCF服務能夠返回JSONP數據,但是即使使用這個簡單的服務,瀏覽到../Service.svc/HelloWorld我也收到了400錯誤請求錯誤。

有人能指出我正確的方向嗎?

+0

什麼是詳細的錯誤信息?這可能是ASP.Net兼容性不支持你的綁定的一部分。 – McGarnagle

+0

服務器在處理請求時遇到錯誤。異常消息是「跨域javascript回調在認證服務中不受支持」。 –

回答

5

看起來像是JSONP,ASP.NET兼容性的組合以及經過驗證的用戶不支持每this Microsoft論壇。

根據論壇的版主,你需要禁用三者之一。

可能不是您希望的答案,但主持人的解釋非常好,並提供了一些建議。

希望這會有所幫助。祝你好運!

+0

謝謝,絕對不是我正在尋找的答案,但無論如何這是一個很好的資源。 –

0

我意識到這已經得到了回答,但它有可能(雖然我不確定是否從安全角度推薦),以便及早通過WebHttpBinding所做的檢查來「解除身份驗證」請求。

其要旨在於,設置HttpContext.Current.User是建立在一個GenericIdentity沒有名稱的新GenericPrincipal或鍵入模仿你會看到什麼,如果未認證用戶剛剛打你的服務 - 通過的WebHttpBinding執行其「沒有認證的時間JSONP調用'檢查請求發生在未經身份驗證的用戶的上下文中。

注:我不確定是否有這樣的安全隱患 - 一個把我的頭頂部的是,如果你有一個身份驗證的用戶他們的會話狀態仍然會提供給你的服務,這可能是一個壞這取決於你在做什麼。

爲此,您可以在幾個地方

  • 通過掛鉤Application.AuthenticateRequest活動由請求URL
  • 過濾通過自定義WCF消息督察

實例消息檢查和行爲因素(同一類,非常有風險使用):

using System; 
using System.Security.Principal; 
using System.ServiceModel; 
using System.ServiceModel.Channels; 
using System.ServiceModel.Configuration; 
using System.ServiceModel.Description; 
using System.ServiceModel.Dispatcher; 
using System.Threading; 
using System.Web; 

namespace MyNamespace 
{ 
    public class ForceAnonymousEndpointBehavior : BehaviorExtensionElement, IDispatchMessageInspector, IEndpointBehavior 
    { 
     public override Type BehaviorType 
     { 
      get { return typeof(ForceAnonymousEndpointBehavior); } 
     } 

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

     object IDispatchMessageInspector.AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) 
     { 
      HttpContext.Current.User = Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("", ""), null); 
      return null; 
     } 

     void IDispatchMessageInspector.BeforeSendReply(ref Message reply, object correlationState) 
     { 

     } 

     void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
     { 
      endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new ForceAnonymousEndpointBehavior()); 
     } 

     void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
     { 

     } 

     void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
     { 

     } 

     void IEndpointBehavior.Validate(ServiceEndpoint endpoint) 
     { 

     } 
    } 
} 

然後在web.config中註冊行爲擴展(在system.serviceModel元素):

<extensions> 
    <behaviorExtensions> 
    <add name="ForceAnonymous" type="MyNamespace.ForceAnonymousEndpointBehavior, MyAssembly" /> 
    </behaviorExtensions> 
</extensions> 

添加行爲到endpointBehavior在問題(再次下system.serviceModel):

<behaviors> 
    <endpointBehaviors> 
    <behavior name="jsonpBehavior"> 
     <ForceAnonymous /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

。 ..並確保在您的服務的端點聲明中通過設置behaviorConfiguration屬性以匹配上面使用的行爲名稱來調用端點行爲。