2011-09-01 72 views
7

我是調用WCF Web服務的小老鼠,所以希望這是一個簡單的問題。當使用.NET 4 winform客戶端調用Web服務時,如何將授權方案從匿名更改爲NTLM?如何使用NTLM授權方案調用Web服務?

現在我收到異常:HTTP請求未經授權,客戶端身份驗證方案爲「匿名」。從服務器收到的驗證頭是'NTLM'

我的目標是構建一個小工具來幫助我監視TFS 2010的數據倉庫和多維數據集。 TFS提供了一個WarehouseControlWebService Web服務。登錄到服務器時,我可以在瀏覽器中通過「測試」模式調用該服務。不過,我試圖從桌面遠程調用相同的Web服務。我的用戶帳戶位於服務器上的本地管理員組中。

我創建了一個帶有規範Button1和TextArea1的.NET 4 WinForm。然後我添加了一個服務引用到Web服務,並創造性地把它稱爲​​ServiceReference1:

Add Service Reference... 
http://tfssvr:8080/tfs/TeamFoundation/Administration/v3.0/WarehouseControlService.asmx 

這裏是我的代碼背後:

private void button1_Click(object sender, EventArgs e) 
{ 
    // Creating a proxy takes about 3-4 seconds 
    var dwSvc = new ServiceReference1.WarehouseControlWebServiceSoapClient(); 

    // Invoking the method throws an MessageSecurityException 
    var dwStatus = dwSvc.GetProcessingStatus(null, null, null); 
} 

我越來越System.ServiceModel.Security.MessageSecurityException:

該HTTP請求未經授權,客戶端身份驗證方案爲「匿名」。從服務器收到的驗證頭是'NTLM'

我試着通過我傳遞憑據:

dwSvc.ClientCredentials.Windows.ClientCredential = 
    new System.Net.NetworkCredential("user", "pass", "domain"); 

,也...

dwSvc.ClientCredentials.Windows.ClientCredential = 
    CredentialCache.DefaultNetworkCredentials; 

我涉水通過WCF文檔,但...哦...男孩有那裏很多。我希望這是一件容易的事情?

在此先感謝。

+0

請參閱http://meta.stackexchange.com/questions/2950/should-hi-thanks -taglines-and-salutations-be-from-posts –

回答

6

設置你的配置綁定 到安全模式= 「TransportCredentialOnly」 和運輸clientCredentialType = 「NTLM」

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="WarehouseControlWebServiceSoap" closeTimeout="00:01:00" 
       openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
       allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
       maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
       messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
       useDefaultWebProxy="true"> 
       <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
        maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
       <security mode="TransportCredentialOnly"> 
        <transport clientCredentialType="Ntlm" proxyCredentialType="None" 
         realm="" /> 
        <message clientCredentialType="UserName" algorithmSuite="Default" /> 
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://tfsServer:8080/tfs/TeamFoundation/Administration/v3.0/WarehouseControlService.asmx" 
      binding="basicHttpBinding" bindingConfiguration="WarehouseControlWebServiceSoap" 
      contract="TfsWarehouse.WarehouseControlWebServiceSoap" name="WarehouseControlWebServiceSoap" /> 
    </client> 
</system.serviceModel> 
+0

我試過使用這種配置,但是,我們的服務器只能通過https訪問,所以我用'wsHttpBinding'和'TransportWithMessageCredential'替換了它。它不起作用,拋出''HTTP請求未經客戶認證方案'匿名'未經授權。從服務器收到的認證頭是'NTLM'。''錯誤。你有什麼建議如何使用'wsHttpBinding'工作? –

+0

我在Visual Studio 2010中得到了這個工作。在Visual Studio的新版本中出現了錯誤。他們生成不同的配置文件。 –