Search code examples
c#wcfsoap.net-4.0wsdl

How to add custom security binding credentials to a web service reference?


I'm new to Service Reference stuff so I might need simple explanation.

I got a WSDL:

https://test.servicebench.com/servicebenchv5/services/CRMServiceOrderService?wsdl

I added the Service Reference in my project via MVS2013 without any problem.

Here's the autogenerated app.config file :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <bindings>
        <customBinding>
            <binding name="CRMServiceOrderBinding">
                <textMessageEncoding messageVersion="Soap11" />
                <httpsTransport />
            </binding>
        </customBinding>
    </bindings>
    <client>
        <endpoint 
            address="https://test.servicebench.com/servicebenchv5/services/CRMServiceOrderService"
            binding="customBinding"
            bindingConfiguration="CRMServiceOrderBinding"
            contract="ServiceBenchReference.CRMServiceOrderPortType" 
            name="CRMServiceOrderPort" />
    </client>
</system.serviceModel>
</configuration>

Now, every requests I try to make to the server replies me : "Unauthorized".

I pretty sure it's because I didn't passed the required credentials in the SOAP request header.

I need to pass it 3 credentials (or info?) : ServiceBenchID, UserID and password.

I've tried using :

CRMServiceOrderPortTypeClient ServiceOrder = new CRMServiceOrderPortTypeClient();
ServiceOrder.ClientCredentials.UserName.UserName = myUserID;
ServiceOrder.ClientCredentials.UserName.password = myPassword;

but it didn't work. I checked the request via Fiddler and I didn't saw the credentials passed in the header.

So here's my question : How can I get these 3 custom credentials information to be passed in the header correctly ? (So I get a response from the server)


Solution

  • After searching a couple more hours, I manage to find a suitable solution !

    Thanks to Thorarin for the piece of code,

    How can I pass a username/password in the header to a SOAP WCF Service

    It seems that having custom bindings in the app.config file is forcing you to add the security elements manually. I don't think its the best way to do it, but its working like this.