Patrick
Patrick

Reputation: 73

.NET SOAP does not send BASIC auth in request header

Wanting to communicate with a SOAP webservice, I had C# classes created by SvcUtil.exe from the wsdl file.

When sending the Request below to a secure server (HTTPS with BASIC auth) I receive a System.ServiceModel.Security.MessageSecurityException and when checking the HTTP request by having traffic go though a Burp proxy I see that no BASIC auth information is passed. Is anything missing for the SOAP request in the C# code or what could be the problem that the BASIC auth does not work?

var binding = new WSHttpBinding();
binding.MessageEncoding = WSMessageEncoding.Mtom;

binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Name = "BasicAuthSecured";

SearchServicePortClient searchClient = new SearchServicePortClient(binding,  new EndpointAddress("https://myUrl:Port/myService"));
searchClient.ClientCredentials.UserName.UserName = "username";
searchClient.ClientCredentials.UserName.Password = "pw";

query soap = new query();
//...

queryResponse response = searchClient.query(soap);

Thanks in advance

Upvotes: 4

Views: 2265

Answers (2)

DanielV
DanielV

Reputation: 2700

This is another approach, don't know if it is the best though

using (var client = _clientFactory.GetClient())
{
    var credentials = Utils.EncodeTo64("user123:password");
    client.ChannelFactory.CreateChannel();
    using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
    {
        var httpRequestProperty = new HttpRequestMessageProperty();
        httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
        //operation
        client.Send(request);
    }
}

Upvotes: 2

Dávid Molnár
Dávid Molnár

Reputation: 11613

Try to use TransportWithMessageCredential:

binding.Security.Mode = SecurityMode.TransportWithMessageCredential;

Upvotes: 0

Related Questions