2014-03-13 43 views
0

我試圖連接到異地asmx服務。我有ASMX網址和登錄密碼進行身份驗證。我已經添加服務引用到服務和VS 2010生成的WCF的客戶端配置:使用登錄密碼認證的WCF客戶端

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="serviceSoap" /> 
     </basicHttpBinding> 
     <customBinding> 
      <binding name="serviceSoap12"> 
       <textMessageEncoding messageVersion="Soap12" /> 
       <httpTransport /> 
      </binding> 
     </customBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://tourml.danko.ru:9191/Service.asmx" 
      binding="basicHttpBinding" bindingConfiguration="serviceSoap" 
      contract="DankoTourMLService.serviceSoap" name="serviceSoap" /> 
     <endpoint address="http://tourml.danko.ru:9191/Service.asmx" 
      binding="customBinding" bindingConfiguration="serviceSoap12" 
      contract="DankoTourMLService.serviceSoap" name="serviceSoap12" /> 
    </client> 

我創建客戶端basicHttpBinding的,並設置憑據。然後我試圖調用服務的方法:

var service = new serviceSoapClient("serviceSoap"); 

service.ClientCredentials.UserName.UserName = username; 
service.ClientCredentials.UserName.Password = password;    

var items = service.GetItemList(); 

但這裏System.ServiceModel.Security.MessageSecurityException拋出:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''. 

與內System.Net.WebException:

{"The remote server returned an error: (401) Unauthorized."} 

ClientCredentials設置有什麼問題?我應該使用另一個綁定,而不是basicHttpBinding?

+0

嘗試像這樣創建你的服務=> var service = new serviceSoapClient();你可能指定了錯誤的綁定。 – woutervs

+0

我已經嘗試了他們兩個,但同樣的錯誤發生 – WabiSabi

回答

0

http://msdn.microsoft.com/en-us/library/ms732391(v=vs.110).aspx 根據以上所述。當您需要指定憑證時,端點應具有安全設置。在您的配置中情況並非如此。

兩種可能性=> addservice引用無法創建正確的配置(不太可能)。 秒=>您必須在服務上而不是在傳輸上指定憑據。

var service = new serviceSoapClient("serviceSoap"); 

//service.ClientCredentials.UserName.UserName = username; 
//service.ClientCredentials.UserName.Password = password;    

service.setCredentials() (or something) 
var itmes = service.getItems(); 

您應該查看服務的文檔。

相關問題