2012-03-05 43 views
1

我創建了一個WCF休息Web服務。然而,客戶已經要求使用基本認證來鎖定服務,但允許他們在第一個響應中提供授權令牌而不是挑戰。不幸的是,我的測試機器上只有IIS 6Restful WCF和通過Fidder進行基本身份驗證

我只真的需要模擬基本身份驗證,所以我通過匿名執行此操作,並在授權令牌不正確時拋出錯誤。但是身份驗證令牌是不可用的WCF

http://localhost/test.svc/get/token/

內容類型:應用程序/ x-WWW窗體-urlencoded

授權:基本Base64Value

如果我刪除匿名的,並且添加基本在IIS中。我得到的是一個401.我猜在IIS做WCF之前的身份驗證。

理想情況下,我只是喜歡惡意訪問,並能夠訪問授權標題。

我怎樣才能獲得身份驗證頭

+0

你想要的方式在你的WCF REST Web服務方法讀取HTTP標頭? – CSharpenter 2012-03-05 13:57:37

回答

1

你擔任這個beeing IIS 6的問題可能是正確的。

我剛剛創建了一個WCF服務「xxx.svc」和託管它的IIS(7.5),當我用fiddler2有正確的授權頭要求它,它沒有發送HTTP 401

我會發布我的代碼,以便在IIS 6上進行測試。如果它仍然爲您提供HTTP 401,那麼這肯定是IIS 6的問題,如果不嘗試對比我的代碼和我的代碼並查看哪些配置是不同的。

的web.config:

<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <bindings> 
     <webHttpBinding> 
     <binding name="webHttpBindConfig"> 
      <security mode="TransportCredentialOnly"> 
      <transport clientCredentialType="Basic" proxyCredentialType="None" /> 
      </security> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <services> 
     <service name="MyTestSvc.MyService"> 
     <endpoint address="http://localhost/TestBasicAuth/Service1.svc" behaviorConfiguration="webHttpEndpointBehavior" 
      binding="webHttpBinding" bindingConfiguration="webHttpBindConfig" 
      name="webHttpBindingEndpoint" contract="MyTestSvc.IMyService" /> 
     <host> 
      <baseAddresses> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="webHttpEndpointBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 

Service1.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MyTestSvc.MyService" CodeBehind="Service1.svc.cs" %> 

IService1.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace MyTestSvc 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IMyService 
    { 

     [OperationContract] 
     [WebGet([email protected]"/Hello")] 
     string GetData(); 

    } 
} 

最後:Service1.svc.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace MyTestSvc 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    public class MyService : IMyService 
    { 
     public string GetData() 
     { 
      WebOperationContext webCtx = WebOperationContext.Current; 
      IncomingWebRequestContext incomingCtx = webCtx.IncomingRequest; 
      string hdrVal = incomingCtx.Headers["Authorization"]; 

      return string.Format("Authorization: {0}", hdrVal); 
     } 
    } 
} 

提琴手結果: Fiddler Composer request

fiddler inspector, request/response raw

+0

這是我在做什麼。但授權頭不存在 – Chris 2012-03-05 21:52:57

+0

並且您已將** basic **設置爲承載您的WCF svc的虛擬目錄的身份驗證模式? – CSharpenter 2012-03-05 22:02:21

+0

是的我現在已經設置了基本的,但是當基本設置時,auth頭部存在,但是當它是base64字符串時,IIS中的步驟會在我的代碼被擊中前導致401。 – Chris 2012-03-06 08:28:07