Home > Mobile >  How to Generate XML With "wsse" Header
How to Generate XML With "wsse" Header

Time:01-26

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?

CodePudding user response:

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.

CodePudding user response:

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.

enter image description here

  • Related