Search code examples
web-servicesactionscript-3apache-flexsoapflashdevelop

SOAP Web Service Auth Headers issue


I am currently trying to use a web service from a Flex client, and hitting a small issue. The service reads a SOAP header called 'AuthHeader' for credentials, and it works from a test client (using Storm from CodePlex) using this request:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthHeader xmlns="http://ns.stickykiwi.com/">
      <Username />
      <Password />
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <Authenticate xmlns="http://ns.stickykiwi.com/" />
  </soap:Body>
</soap:Envelope>

The service returns true (Boolean) if the credentials are ok, false if not. Now when I call the service from Flex it sends this:

<?xml version="1.0" encoding="utf-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Header>
        <ns0:AuthHeader xmlns:ns0="https://stickykiwi01">
            <ns0:Password/>  
            <ns0:Username/>
        </ns0:AuthHeader>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <tns:Authenticate xmlns:tns="http://ns.stickykiwi.com/"/>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

This returns,

'System.NullReferenceException: Object reference not set to an instance of an object'

because for some reason, probably best known to someone with better eyesight than me, there is a serious difference between these 2 requests, I am just not seeing it.

So, how do I create the headers in Flex? I have this code in the web service definition file:

    var qname:QName = new QName("https://stickykiwi01","AuthHeader");
    var header:SOAPHeader = new SOAPHeader(qname,{Username:"",Password:""});
    _serviceControl.addHeader(header);

Which is in a file called _Super_AuthenticationServices.as. I pulled the Flex request from the network monitor.

A couple of points to note,

  1. The service isn't mine, but I assume it works as I can test it from another client successfully.
  2. Yes I know SOAP services are now depreciated and we should be moving all our code to WCF, it is in progress and will be brought in house eventually.
  3. We wanted to use Basic auth for the service but Flex doesn't allow me to add the HTTP headers to a SOAP request so that is out.
  4. All of this is run over HTTPS, because I know it is not secure as credentials are passed as plain text, once we have this running we will pass a single hashed string (same as basic auth) and unhash it on the server too, this way is just MUCH easier to debug.

Thank you for your advice.

EDIT

This is what the service is expecting, and I think I am meeting that criteria, I cannot see an issue here at all.

POST /services/_authentication/authenticationservices.asmx HTTP/1.1
Host: stickykiwi01
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://ns.stickykiwi.com/Authenticate"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Header>
          <AuthHeader xmlns="http://ns.stickykiwi.com/">
              <Username>string</Username>
              <Password>string</Password>
          </AuthHeader>
      </soap:Header>
      <soap:Body>
          <Authenticate xmlns="http://ns.stickykiwi.com/" />
      </soap:Body>
  </soap:Envelope>

Solution

  • No, it works.

    Not sure what was going on, but I changed nothing, but this morning the headers are working exactly as expected, here is the request

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SOAP-ENV:Header>
          <tns:AuthHeader xmlns:tns="http://ns.stickykiwi.com/">      
              <tns:Username></tns:Username>      
              <tns:Password></tns:Password>
          </tns:AuthHeader>
      </SOAP-ENV:Header>
      <SOAP-ENV:Body>
          <tns:Authenticate xmlns:tns="http://ns.stickykiwi.com/"/>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    The only difference I can see is that now the prefix is tns on everything instead of ns0. But anyway, issue resolved, possibly via magic.

    Thanks