简体   繁体   中英

How to Generate XML With "wsse" Header

we are using asp Core to generate XML Code to be sent to our WSDL Provider, you can check the sample below that we manually wrote it, and its working just fine when we are trying to form the PostMan, so we tried to generate the XML programmatically, we got the same XML But some tags missed like "wsse", please do check the sample below

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soap:mustUnderstand="1">
         <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="XXXX">
            <wsse:Username>XXX</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXX</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soap:Header>
   <soap:Body xmlns:ns1="http://www.opentravel.org/OTA/2003/05">
      <ns1:OTA_AirAvailRQ EchoToken="11868765275150-1300257933" PrimaryLangID="en-us" SequenceNmbr="1" TimeStamp="2012-08-27T03:00:23" Version="20061.00" Target="TEST">
         <ns1:POS>
            <ns1:Source TerminalID="XXXX">
               <ns1:RequestorID ID="XXXX" Type="XXXX" />
               <ns1:BookingChannel Type="12" />
            </ns1:Source>
         </ns1:POS>
         <ns1:OriginDestinationInformation>
            <ns1:DepartureDateTime WindowAfter="P1D" WindowBefore="P1D">2023-01-20T10:00:00</ns1:DepartureDateTime>
            <ns1:OriginLocation LocationCode="BOM" />
            <ns1:DestinationLocation LocationCode="SHJ" />
         </ns1:OriginDestinationInformation>
         <ns1:OriginDestinationInformation>
            <ns1:DepartureDateTime WindowAfter="P1D" WindowBefore="P1D">2023-01-25T10:00:00</ns1:DepartureDateTime>
            <ns1:OriginLocation LocationCode="SHJ" />
            <ns1:DestinationLocation LocationCode="BOM" />
         </ns1:OriginDestinationInformation>
         <ns1:TravelerInfoSummary>
            <ns1:AirTravelerAvail>
               <ns1:PassengerTypeQuantity Code="ADT" Quantity="3" />
               <ns1:PassengerTypeQuantity Code="CHD" Quantity="1" />
            </ns1:AirTravelerAvail>
         </ns1:TravelerInfoSummary>
      </ns1:OTA_AirAvailRQ>
   </soap:Body>
</soap:Envelope>

but when we are trying to generate it we getting this sample

<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap="http://schemas.xmlsoap.org/soap/envelope/" xsd="http://www.w3.org/2001/XMLSchema" xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opentravel.org/OTA/2003/05">
    <Header>
        <Security mustUnderstand="1" wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <UsernameToken Id="xxx" wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xs">
                <Username>xx</Username>
                <Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxx</Password>
            </UsernameToken>
        </Security>
    </Header>
    <Body ns2="http://www.opentravel.org/OTA/2003/05">
        <OTA_AirAvailRQ EchoToken="11868765275150-1300257933" PrimaryLangID="en-us" SequenceNmbr="1" Target="xx" TimeStamp="2023-01-14T12:01:53.3258485+03:00" Version="20061">
            <POS>
                <Source TerminalID="xx">
                    <RequestorID ID="xx" Type="4" />
                    <BookingChannel Type="12" />
                </Source>
            </POS>
            <OriginDestinationInformation>
                <DepartureDateTime WindowAfter="P1D" WindowBefore="P1D">2023-01-20T10:00:00</DepartureDateTime>
                <OriginLocation LocationCode="BOM" />
                <DestinationLocation LocationCode="SHJ" />
            </OriginDestinationInformation>
            <TravelerInfoSummary>
                <AirTravelerAvail>
                    <PassengerTypeQuantity Code="ADT" Quantity="1" />
                </AirTravelerAvail>
            </TravelerInfoSummary>
        </OTA_AirAvailRQ>
    </Body>
</Envelope>

So Can you please let us know how we can generate the XML With these "wsse" tags?

My First Attempt with the code was this and I am getting this error from it "Compiling JScript/CSharp scripts is not supported", we are trying with ASP CORE Web API 3.1

            System.ServiceModel.BasicHttpBinding binding =
            new System.ServiceModel.BasicHttpBinding();
            binding.MaxBufferSize = int.MaxValue;
            binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.AllowCookies = true;
            binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
            binding.TransferMode = System.ServiceModel.TransferMode.Buffered;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            ServiceHelper _ServiceHelper = new ServiceHelper();
            var address = new EndpointAddress("https://airarabia.isaaviations.com/webservices/services/AAResWebServices");
            var client = new AAResWebServicesClient((Binding)binding, address);

            client.ClientCredentials.UserName.UserName = _ServiceHelper.airarabia9GUserName;
            client.ClientCredentials.UserName.Password = _ServiceHelper.airarabia9Gpassword;
            OTA_AirAvailRQ test = new OTA_AirAvailRQ();
            OTA_AirAvailRS TEST2;
            try
            {
                var httpHeaders = ReturnHttpHeader(_ServiceHelper.airarabia9GUserName, _ServiceHelper.airarabia9Gpassword);
                client.Endpoint.EndpointBehaviors.Add(new HttpHeadersEndpointBehavior(httpHeaders));
                //  TEST2 = client.getAvailability (test);
                TEST2 = client.getAvailability(test);
            }
            catch (Exception se)
            {
                Console.WriteLine("Error : " + se.Message);
                client.Abort();
                return null;
            }

and my second attempt with the code is that I created the Modules manually based on the Samples and try to convert them into XML But also failed which I think this is wrong oprtion from the first place

//envelope Containt all the modules and will convert them into XML
            var stringwriter = new System.IO.StringWriter();
            var serializer = new XmlSerializer(typeof(Envelope),
                             "http://www.opentravel.org/OTA/2003/05");
            serializer.Serialize(stringwriter, envelope);
            string strXML = stringwriter.ToString();
            strXML = strXML.Replace("utf-16", "utf-8");

            var url = _Helper.airarabia9GURL;
            var client2 = new RestClient(url);
            var request = new RestRequest(url, Method.POST);



            #region Headers
            request.AddHeader("Content-Type", "application/xml");
            request.AddHeader("Cookie", "JSESSIONID=0F63BEC1C68DDEC6ADB581CC621E1B8E.demo2144");

            #endregion


            request.AddParameter("application/xml", Mybody, ParameterType.RequestBody);
            IRestResponse response = client2.Execute(request);
            Console.WriteLine(response.Content);

FYI: We are using the same project to integrate different API Like Travelport, Turkish, QR AirLine etc and they are working fine

wsse is a namespace prefix to the element name. The prefix is "bound"/defined/identified via xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" . I don't know what code was written to produce the "incomplete"/undesired current XML output, but can imagine you can use some method or option to create elements with namespace declarations and elements with a bound namespace prefix to them. Maybe System.Xml.XmlDocument.CreateElement(String, String) or System.Xml.XmlDocument.CreateElement(String, String, String) of .NET (the overloaded variants with 2/3 String parameters). But I'm just guessing.

I had a test with code below,

public async Task<IActionResult> GetAsync() {
    UserModel user = new UserModel();
    user.userName = "user1";
    user.Password = "pwd";
    
    var ns = new XmlSerializerNamespaces();
    ns.Add("", "");
    ns.Add("wsse", "http://aa.com");
    ns.Add("bbb", "http://bb.com");

    var ser = new XmlSerializer(user.GetType());
    var res = new System.IO.StringWriter();
    ser.Serialize(res, user, ns);
    return new ContentResult
    {
        Content = res.ToString(),
        ContentType = "application/xml",
        StatusCode = 200
    };
}

namespace WebApi.Models
{
    [XmlRoot(Namespace = "http://bb.com")]
    public class UserModel
    {
        [XmlElement(Namespace = "http://aa.com")]
        public string? userName { get; set; }
        [XmlElement(Namespace = "http://aa.com")]
        public string? Password { get; set; }
    }
}

And the test result is, the node which defined the same Namespace will be added the prefix.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM