2014-01-15 89 views
2

應用程序SoapUI我爲端點設置了密碼和用戶名。當您調用該方法時,這些數據不會在消息頭中傳輸,而會在其外部傳輸(Authorization: Basic YTph)。但是,在這種情況下,呼叫被成功認證。如果我嘗試在我的c#客戶端應用程序的標題中添加身份驗證數據,則該服務不會看到用戶名。授權:SOAP服務客戶端中的基本

了SoapUI:

POST https://xxx.xx:9444/xx HTTP/1.1 
Accept-Encoding: gzip,deflate 
Content-Type: text/xml;charset=UTF-8 
SOAPAction: "" 
Authorization: Basic YTph 
Content-Length: 470 
Host: xx.loc:9444 
Connection: Keep-Alive 
User-Agent: Apache-HttpClient/4.1.1 (java 1.5) 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ami="http://xxx.pl"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ami:getMeterPointStates> 
     <addr> 
      <ppec>0</ppec> 
      <mid>0</mid> 
     </addr> 
     <start>2012-04-01T00:00:00</start> 
     <stop>2012-05-01T00:00:00</stop> 
     <hard>false</hard> 
     </ami:getMeterPointStates> 
    </soapenv:Body> 
</soapenv:Envelope> 

C#客戶samle代碼:

System.Net.ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate); 
CommunicationWebserviceClient client = new CommunicationWebserviceClient(); 

client.ClientCredentials.UserName.UserName = "user"; 
client.ClientCredentials.UserName.Password = "password"; 

using (new System.ServiceModel.OperationContextScope(client.InnerChannel)) 
{ 
    MessageHeader head = MessageHeader.CreateHeader("Authorization", "http://xxx.pl", "Basic YTph"); 
    OperationContext.Current.OutgoingMessageHeaders.Add(head); 

    MeterPointAddr addr = new MeterPointAddr(); 
    addr.ppec = "0"; 
    addr.mid = "0"; 

    var response = client.getMeterPointStates(addr, new DateTime(2012, 4, 1), new DateTime(2012, 4, 14), false, "", measurementsFlagEnum.REAL_ESTIMATION); 
} 

的App.config:

<bindings> 
    <customBinding> 
     <binding name="CommunicationWebservicePortBinding"> 
      <textMessageEncoding messageVersion="Soap11" /> 

      <httpsTransport authenticationScheme="Basic" /> 
     </binding> 
    </customBinding> 
</bindings> 
<client> 
    <endpoint address="https://xxx.xx:9444/xx" 
     binding="customBinding" bindingConfiguration="CommunicationWebservicePortBinding" contract="AMIService.CommunicationWebservice" 
     name="CommunicationWebservicePort" /> 
</client> 

如何添加C#編寫的一個客戶端(Authorization: Basic YTph)頭?

+0

我猜你的C#的客戶端代碼,你並不需要添加'MessageHeader.CreateHeader(「授權」 ...'線解決方案。當你將認證設置爲basic並且使用用戶名和密碼時,這條線是自動生成的.YTph'實際上是你的用戶名和密碼,base-64編碼。如果這是產品代碼,請確保使用SSL。 – oleksii

+0

它工作正常對我來說 http://stackoverflow.com/questions/20495105/basic-authentication-in-php-webservice-with -c/20517700#20517700 有一個代碼示例。 – Paulo

回答