Calling a web service that requires basic http authentication from wcf client
I have a wsdl from a web service, I generated the wcf proxy. No problem.
But I can not get my head around how to pass the user name and password. The webservice requires basic authentication - only username and password.
Any help ?
Is Basic authentication configured in configuration file? Do you need to pass only credentials or do you also need secured transport (HTTPS)?
First you need to set up binding to support Basic authentication
Setup for HTTP binding:
<bindings> <basicHttpBinding> <binding name="BasicAuth"> <security mode="TransportCredentialOnly"> <transport clientCredentialType="Basic" /> </security> </binding> </basicHttpBinding> </bindings>
Setup for HTTPS binding:
<bindings> <basicHttpBinding> <binding name="BasicAuthSecured"> <security mode="Transport"> <transport clientCredentialType="Basic" /> </security> </binding> </basicHttpBinding> </bindings>
Client endpoint has to use defined configuration like:
<client> <endpoint address="..." name="..." binding="basicHttpBinding" bindingConfiguration="BasicAuth" contract="..." /> </client>
Then you have to pass credentials to the proxy:
proxy = new MyServiceClient(); proxy.ClientCredentials.UserName.UserName = "..."; proxy.ClientCredentials.UserName.Password = "...";
This should cover it: http://msdn.microsoft.com/en-us/library/ms733775.aspx (See the Client section)
I would say it is likely to depend on how the web service expects you to pass the information. After all, you are only the consumer.
Having said that, it is common is web services to have the userid and password passed in the SOAP Header.
You can refer to this link for a sample implementation of this scenario
Sample Soap Message
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <AUTHHEADER xmlns="http://tempuri.org/"> <USERNAME>string</USERNAME> <PASSWORD>string</PASSWORD> </AUTHHEADER> </soap:Header> <soap:Body> <SENSITIVEDATA xmlns="http://tempuri.org/" /> </soap:Body> </soap:Envelope>