Adding a header to a simple web service request

I have a console application, I add a link to the website, just clicking and using "Add link to service", then my .Config file changed to:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="FServiceSoapBinding" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://ServiceAdd/FService" binding="basicHttpBinding"
                bindingConfiguration="FServiceSoapBinding" contract="MyReference.MService_"
                name="FServicePort" />
        </client>
    </system.serviceModel>
</configuration>

So, everything is fine, and it seems that I can successfully use this service, but in the service documentation I need to set the username and password in the request header, and this is an example of the documentation:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://namespace.com/">
  <soapenv:Header>
    <Account>
      <username>user</username>
      <password>password</password>
    </Account>
  </soapenv:Header>
  <soapenv:Body>
    <web:oneofservicemethods>
      <items>
        <item>...</item>
      </items>
    </web:oneofservicemethods>
  </soapenv:Body>
</soapenv:Envelope>

So how can I add a username and password to the header? any suggestion?

+2
source share
2 answers

If I understand correctly, you want to set the username and password in the request header. You can achieve with help WCF Extensibility – Message Inspectorswith IClientMessageInspector.BeforeSendRequest.

, IClientMessageInspector. BeforeSendRequest .

. this, this this

0

.config, :

<client>
    <endpoint address="http://ServiceAdd/FService" binding="basicHttpBinding"
     bindingConfiguration="FServiceSoapBinding" contract="MyReference.MService_"
     name="FServicePort" >
        <headers>
            <Account>
                <username>user</username>
                <password>password</password>
            </Account>
        </headers>
    </endpoint>
</client>
+2

All Articles