Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom SOAPHeader in C# for a web service call

Tags:

c#

soapheader

I am trying to add a custom soap header information in c# before calling a web service. I am using SOAP Header class to get this done. I could do this partly but not completely the way I need it. Here is how I need the soap header to look like

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header>
      <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <UsernameToken>
         <Username>USERID</Username>
         <Password>PASSWORD</Password>
        </UsernameToken>    
      </Security>
   </soap:Header>
   <soap:Body>
   ...

I am able to add soap header as below

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header>
      <UsernameToken xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <Username>UserID</Username>
         <Password>Test</Password>
      </UsernameToken>
   </soap:Header>
   <soap:Body>

What I am not able to do is add the "Security" elements which wraps the "UsernameToken" as in the first sample. Any help would be appreciated.

like image 265
user2810857 Avatar asked Sep 24 '13 11:09

user2810857


1 Answers

This link adding soap header worked for me. I am calling a SOAP 1.1 service that I did not write and have no control over. Am using VS 2012 and added the service as a web reference in my project. Hope this helps

I followed the steps 1-5 from J. Dudgeon's post towards the bottom of the thread.

Here's some sample code (this would be in a separate .cs file):

namespace SAME_NAMESPACE_AS_PROXY_CLASS
{
    // This is needed since the web service must have the username and pwd passed in a custom SOAP header, apparently
    public partial class MyService : System.Web.Services.Protocols.SoapHttpClientProtocol
    {
        public Creds credHeader;  // will hold the creds that are passed in the SOAP Header
    }

    [XmlRoot(Namespace = "http://cnn.com/xy")]  // your service's namespace goes in quotes
    public class Creds : SoapHeader
    {
        public string Username;
        public string Password;
    }
}

And then in the generated proxy class, on the method that calls the service, per J Dudgeon's step 4 add this attribute: [SoapHeader("credHeader", Direction = SoapHeaderDirection.In)]

finally, here's the call to the generated proxy method, with the header:

using (MyService client = new MyService())
{
    client.credHeader = new Creds();
    client.credHeader.Username = "username";
    client.credHeader.Password = "pwd";
    rResponse = client.MyProxyMethodHere();
}
like image 174
PBMe_HikeIt Avatar answered Oct 20 '22 10:10

PBMe_HikeIt