2017-02-06 30 views
0

我寫了一個非常簡單的WFCSerice,它返回提供的Windows用戶名。下面是客戶端代碼:基本身份驗證似乎沒有安全標頭

public Form1() 
     { 
      ServiceReference1.Service1Client s1 = new ServiceReference1.Service1Client(); 
      s1.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials; 
      string str = s1.ReturnWindowsUsername(); 
      InitializeComponent(); 
     } 

我可以用看的HTTP頭的憑證Fidddler:

enter image description here

我試圖做同樣的事情基本身份驗證(訪問另一個網站支持基本認證的服務)。下面是客戶端代碼:

public Form1() 
     { 
      InitializeComponent(); 
      ServiceReference1.Service1Client s1 = new ServiceReference1.Service1Client(); 
      s1.ClientCredentials.UserName.UserName = "testuser"; 
      s1.ClientCredentials.UserName.Password = "testpassword"; 
      string str = s1.GetData(1); 

     } 

下面是一個使用基本身份驗證時,從提琴手截圖:

enter image description here

爲什麼使用基本身份驗證時,有沒有在報頭。基本身份驗證服務似乎按預期工作。這裏是響應(有趣的是,似乎有兩個請求和兩個響應):

enter image description here

+0

您是否已禁用匿名身份驗證,並且基本身份驗證已啓用在IIS中編輯?授權標頭僅在來自服務器的401挑戰之後被添加到請求*中。如果匿名允許請求,則不存在401,因此不需要標題。 – user1429080

+0

@ user1429080,都啓用。 – w0051977

+0

嘗試禁用匿名身份驗證。這有什麼不同嗎? – user1429080

回答

1

基本身份驗證工作於HTTP水平。一般流程是客戶端請求資源,然後服務器發出挑戰,然後客戶端發出一個包含Authorization頭的新請求。如果服務器接受到Authorization標題中的用戶名和密碼,則客戶端通常會爲後續請求添加標題,而無需再次執行request - challenge - re-request-with-authorization步驟。

如果您的所有設置都正確,您應該期望在Fiddler中看到兩個請求。

  1. 一個請求沒有Authorization頭包括在內。服務器對此請求的響應將爲401,並附帶一個WWW-Authenticate: Basic realm="your realm"標頭。
  2. 然後,您應該看到第二個請求,其中Authorization標頭已從客戶端發送。

這裏是我的環境樣本:

enter image description here

如果不從服務器上看到401挑戰,那麼基本身份驗證設置不正確了。

爲了使服務代理提供標題,您需要將客戶端綁定配置爲使用<transport clientCredentialType="Basic"/>。或者這就是我所做的,誰知道WCF與它的無數配置選項。

編輯:我用這個在服務端:

<bindings> 
    <basicHttpBinding> 
    <binding name="httpTransportCredentialOnlyBinding"> 
     <security mode="TransportCredentialOnly"> 
     <transport clientCredentialType="Basic" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

在客戶端:

<bindings> 
    <basicHttpBinding> 
    <binding name="BasicHttpBinding_IService1"> 
     <security mode="TransportCredentialOnly"> 
     <transport clientCredentialType="Basic"/> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 
<client> 
    <endpoint address="http://localhost:53156/Service1.svc" binding="basicHttpBinding" 
    bindingConfiguration="BasicHttpBinding_IService1" contract="WcfTest_CBT.IService1" 
    name="BasicHttpBinding_IService1" /> 
</client> 

我爲了不SSL麻煩方便地測試這個使用basicHttpBindingTransportCredentialOnlyBasic等等

+0

謝謝。我嘗試了你的建議。標題中仍然沒有安全性。基本身份驗證正如我所料,但我完全不知道如何將憑據傳遞到服務器。 – w0051977

+0

不,我不知道。它是否必須是BasicHttpBinding才能在頭中傳遞證書?我的研究告訴我憑證是通過wsHttpBinding在SOAP消息(加密的)中傳遞的。我對此很新。 – w0051977

+0

使用什麼綁定使用?什麼是安全模式? – w0051977