3

I'm writing a client against a customer's SOAP service, using WCF.

We've had a number of go-arounds trying to get the authentication working. I ended up using a Custom Binding, because some random guy on the web said that BasicHttpBinding didn't support the necessary security options, and WsHttpBinding didn't support SOAP 1.1, which is what they are using.

So, what I have:

var message = this.constructMessagecollection);

if (message != null)
{
    var ea = new EndpointAddress(this.webServiceUrl);

    var binding = new CustomBinding();
    binding.Elements.Add(new TextMessageEncodingBindingElement(
            MessageVersion.Soap11, Encoding.UTF8));
    binding.Elements.Add(new HttpsTransportBindingElement { AuthenticationScheme = System.Net.AuthenticationSchemes.Basic });

    using (var client = new CustomersWebserviceClient(binding, ea))
    {
        if (!String.IsNullOrWhiteSpace(this.webServiceUsername) && !String.IsNullOrWhiteSpace(this.webServicePassword))
        {
            var credentials = client.ClientCredentials.UserName;
            credentials.UserName = this.webServiceUsername;
            credentials.Password = this.webServicePassword;
        }

        var result = client.ReceiveMessage(message);
        log.writeLine(String.Format("Call to client.ReceiveMessage() returned {0}", result));
    }

    return true;
}

Now, I've been asked if I can configure my client to do preemptive authentication. I've done some web browsing, and not found much. And I'm at a loss as to how to integrate what little I've found into my current code.

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165

1 Answers1

14

I don't think you can configure WCF to pre authenticate. Your options are to add the headers manually to each request or to build a message inspector to do it and configure it once. Either way those settings are not related to the binding. I guess you could write your own custom http transport (that internally uses the regular http transport) and add it there but not sure it worth the effort. As described here, to add it manually you can use:

HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); 

httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + 
    Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + 
    client.ClientCredentials.UserName.Password));

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) 
{ 
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = 
        httpRequestProperty;

    // Invoke client 
}

As for the second option, this see here how to add headers with a message inspector.

Community
  • 1
  • 1
Yaron Naveh
  • 23,560
  • 32
  • 103
  • 158
  • When I try that, I get an exception on the client invocation: "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state." I do not get that error when I'm not adding my own headers. – Jeff Dege Oct 21 '14 at 15:04
  • 1
    Never mind. I was passing the Basic authentication header instead of setting ClientCredentials. When I do both, that error goes away. – Jeff Dege Oct 21 '14 at 15:11