3

The application SoapUI I set a password and user name for the endpoint. When you call the method, these data are not transmitted in the message header but outside of it (Authorization: Basic YTph). However, in this case the call is successfully authenticated. If I try to add the authentication data in the header in my c# client application, the service does not see user names.

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# Client samle code:

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>

How do I add a client written in c # that (Authorization: Basic YTph) header?

Emil J
  • 215
  • 2
  • 4
  • 20
  • I'd guess in your C# client code, you don't need to add `MessageHeader.CreateHeader("Authorization" ...` line. This line is generated automatically when you set authentication to basic and use user name with password. The `YTph` is effectively your user name and password, base-64 encoded. Make sure to use SSL if this is production code. – oleksii Jan 15 '14 at 11:12
  • It worked for me. http://stackoverflow.com/questions/20495105/basic-authentication-in-php-webservice-with-c/20517700#20517700 Have an example of code. – Paulo Jan 15 '14 at 11:19

1 Answers1

1

I found solution in other question :)

How to add custom Http Header for C# Web Service Client consuming Axis 1.4 Web service

Community
  • 1
  • 1
Emil J
  • 215
  • 2
  • 4
  • 20