I wanted to consume a java webservice, so I wrote a WCF client to consume the service. I thought Java -.Net interinteroperability would be easy with WCF, not so easy.
The Java Web Service used WS-Security and required a UserNameToken passed as clear text. The below code snippet from Hanselman's blog will solve the problem, but there is a catch . This will work only if the endpoint address is on SSL. Ofcourse, WCF will not allow you to pass UserNameToken ( username & password) as clear text on http. It has to passed on https protocol, only then it will work .
public void WCFMethod()
{
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode
= SecurityMode.TransportWithMessageCredential;
myBinding.Security.Message
.ClientCredentialType = MessageCredentialType.UserName;
myBinding.Security
.Message.EstablishSecurityContext = false;
myBinding.Security
.Message.NegotiateServiceCredential = false;
BindingElementCollection elements = myBinding.CreateBindingElements();
elements
.Find<SecurityBindingElement>().IncludeTimestamp = false;
elements
.Find<TextMessageEncodingBindingElement>()
.MessageVersion = MessageVersion.Soap11;
CustomBinding newBinding = new CustomBinding(elements);
FooPortTypeClient svc =
new FooPortTypeClient(newBinding,
new EndpointAddress("https://javawebservice.com/service"));
}
So, I had to go apply SSL on the webservice, to get this done. I digged more & was wondering if I could create a custom binding by getting my hands dirty. It turned out, there is already an open source custom binding, luckily I stumbled on Yaron Naveh's blog. He has writtern Clear Username Binding to solve the problem and is hosted ongoogle code. I hope this helps others like me. I used this binding get authenticated on http using WS-Security.