Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining a value within a SOAP Header from the OperationContext

I have the following code in C# that looks for an apiKey in the the following SOAP header:

SOAP Header:

<soap:Header>
   <Authentication>
       <apiKey>CCE4FB48-865D-4DCF-A091-6D4511F03B87</apiKey>
   </Authentication>
</soap:Header>

C#:

This is what I have so far:

public string GetAPIKey(OperationContext operationContext)
{
    string apiKey = null;

    // Look at headers on incoming message.
    for (int i = 0; i < OperationContext.Current.IncomingMessageHeaders.Count; i++)
    {
        MessageHeaderInfo h = OperationContext.Current.IncomingMessageHeaders[i];

        // For any reference parameters with the correct name.
        if (h.Name == "apiKey")
        {
            // Read the value of that header.
            XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
            apiKey = xr.ReadElementContentAsString();
        }
    }

    // Return the API key (if present, null if not).
    return apiKey;
}

PROBLEM: Returning null instead of the actual apiKey value:

CCE4FB48-865D-4DCF-A091-6D4511F03B87

UPDATE 1:

I added some logging. It looks like h.Name is in fact "Authentication", which means it won't actually be looking for "apiKey", which then means it won't be able to retrieve the value.

Is there a way to grab the <apiKey /> inside of <Authentication />?

UPDATE 2:

Ended up using the following code:

if (h.Name == "Authentication")
{
  // Read the value of that header.
  XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
  xr.ReadToDescendant("apiKey");
  apiKey = xr.ReadElementContentAsString();
}
like image 871
fulvio Avatar asked Jun 21 '11 07:06

fulvio


People also ask

How do I pass a SOAP request header?

Select the service task or web service integration component and click the Variables tab above the diagram area. Create the private variable that you will later map to the SOAP header of the request message. To add a single header entry to the request message, use the variable type SOAPHeader.

What is header in SOAP request?

The SOAP header is an optional section in the SOAP envelope, although some WSDL files require that a SOAP header is passed with each request. A SOAP header contains application-specific context information (for example, security or encryption information) that is associated with the SOAP request or response message.

What is the difference between SOAP header and HTTP header?

HTTP headers usually stop at the web server while the SOAP message in it's entirety will be passed downstream to the ultimate receiver who needs the data (maybe even passing through more intermediaries who they might also need it).

How do I add a SOAP header in WSDL?

You can populate a SOAP header in the following ways: Define the SOAP header in the WSDL definition as part of the SOAP binding. You indicate these headers by using a <soap:header> tag in the <wsdl:input> and <wsdl:output> elements in the WSDL file.


2 Answers

I think your h.Name is Authentication because it is root type and apiKey is property of Authentication type. Try logging values of h.Name to some log file and check what does it return.

if (h.Name == "Authentication")
{
    // Read the value of that header.
    XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
    //apiKey = xr.ReadElementContentAsString();
    xr.ReadToFollowing("Authentication");
    apiKey = xr.ReadElementContentAsString();
}
like image 196
Akash Kava Avatar answered Nov 09 '22 23:11

Akash Kava


Ended up using the following code:

if (h.Name == "Authentication")
{
  // Read the value of that header.
  XmlReader xr = OperationContext.Current.IncomingMessageHeaders.GetReaderAtHeader(i);
  xr.ReadToDescendant("apiKey");
  apiKey = xr.ReadElementContentAsString();
}
like image 22
fulvio Avatar answered Nov 09 '22 22:11

fulvio