Need help adding authentication heading to a soap/xml web request

Bob Alston 126 Reputation points
2023-03-30T20:53:24.04+00:00
I am using this process:
https://stackoverflow.com/questions/38058579/how-to-create-soap-xml-client-from-wsdl-in-console-cron-job-app-c-sharp-vs2012
The WSDL is this:

https://cmbhs.dshs.state.tx.us/cmbhswebservice/Service/DataDownloadService.asmx?wsdl

I get the classes generated and think I have the following code to invoke the methods, which I think is correct.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services.Description;

namespace ConsoleApp7
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var service = new us.tx.state.dshs.cmbhs.DataDownloadService();

            DateTime FromDate = new DateTime(2022, 10, 01);
            DateTime   ToDate = new DateTime(2022, 10, 03);
            var mydata = service.Download(449737,FromDate ,ToDate , 12, 02);
            Console.WriteLine(mydata);
        }
    }
}

After MUCH reading I have determined that the authentication ID and password are not part of the XML but 
are required.

I found this web post about how to insert authentication in to the header of a web service request:

https://stackoverflow.com/questions/45103756/how-can-i-add-a-soap-authentication-header-with-httprequestmessage

I have ended up with this code but can't seem to get the references correct:

using ConsoleApp7.us.tx.state.dshs.cmbhs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services.Description;

namespace ConsoleApp7
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var service = new us.tx.state.dshs.cmbhs.DataDownloadService();
          //  using (new OperationContextScope(client.InnerChannel))
            DateTime FromDate = new DateTime(2022, 10, 01);
            DateTime   ToDate = new DateTime(2022, 10, 03);///var mydata = ser
           // Add a SOAP Header to an outgoing request
           ///// MessageHeader aMessageHeader = MessageHeader.CreateHeader("CMBHSAuthenticationHeader", "http://www.dshs.state.tx.us/cmbhs/", "     <CMBHSUserName>xxxx</CMBHSUserName>\r\n      <CMBHSPassword>yyyy</CMBHSPassword>");
           //// OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);
            var mydata = service.Download(449737,FromDate ,ToDate , 12, 02);
            Console.WriteLine("----START----");
            Console.WriteLine(mydata);
            Console.WriteLine("----END----");
        }
    }
}

Note the two commented lines of code which are after the line "// Add a SOAP Header..."

I cannot seem to get these two lines correct.  Can anyone help?

Bob

P.S. 
For reference, this is the code I have used with another language and process to directly send the request.  It has worked great for several years.

<?xml version="1.0" encoding="utf-8"?>
<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>
    <CMBHSAuthenticationHeader xmlns="http://www.dshs.state.tx.us/cmbhs/">
      <CMBHSUserName>xxxx</CMBHSUserName>
      <CMBHSPassword>yyyy</CMBHSPassword>
    </CMBHSAuthenticationHeader>
  </soap:Header>
  <soap:Body>
    <Download xmlns="http://www.dshs.state.tx.us/cmbhs/">
        <organizationNbr>479</organizationNbr>
        <fromDate>##########</fromDate>
        <toDate>$$$$$$$$$$</toDate>
        <downloadType>##</downloadType>
        <downloadDateType>2</downloadDateType>
    </Download>
  </soap:Body>
</soap:Envelope>

Note: I grogramatically update the place holder for fromDate, Todate and download type.


Developer technologies .NET Other
Developer technologies C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.