Sample code for 30+ languages & platforms
Unicode C++

SOAP WS-Security Username Authentication

See more XML Examples

Demonstrates creating SOAP XML for WS-Security Username Authentication. The client user name and password are encapsulated in a WS-Security <wsse:UsernameToken>.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkXmlW.h>
#include <CkDateTimeW.h>

void ChilkatSample(void)
    {
    // Generate this XML with the code that follows:

    // 	<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    // 	 <soap:Header>	     
    // 	  <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2003/06/secext">
    // 	   <wsse:UsernameToken wsu:Id="sample" 
    // 	       xmlns:wsu="http://schemas.xmlsoap.org/ws/2003/06/utility">
    // 	    <wsse:Username>sample</wsse:Username>
    // 	    <wsse:Password Type="wsse:PasswordText">oracle</wsse:Password>
    // 	    <wsu:Created>2004-05-19T08:44:51Z</wsu:Created>
    // 	   </wsse:UsernameToken>
    // 	  </wsse:Security>
    // 	  <wsse:Security soap:actor="oracle" 
    // 	      xmlns:wsse="http://schemas.xmlsoap.org/ws/2003/06/secext">
    // 	   <wsse:UsernameToken wsu:Id="oracle" 
    // 	       xmlns:wsu="http://schemas.xmlsoap.org/ws/2003/06/utility">
    // 	    <wsse:Username>myUsername</wsse:Username>
    // 	    <wsse:Password Type="wsse:PasswordText">myPassword</wsse:Password>
    // 	    <wsu:Created>2004-05-19T08:46:04Z</wsu:Created>
    // 	   </wsse:UsernameToken>
    // 	  </wsse:Security>
    // 	 </soap:Header>
    // 	  <soap:Body>
    // 	   <getHello xmlns="http://www.oracle.com"/>
    // 	  </soap:Body>
    // 	</soap:Envelope>
    // 

    // First build the outer housing:
    CkXmlW xml;
    xml.put_Tag(L"soap:Envelope");
    xml.AddAttribute(L"xmlns:soap",L"http://schemas.xmlsoap.org/soap/envelope/");
    xml.UpdateChildContent(L"soap:Header",L"");
    xml.UpdateAttrAt(L"soap:Body|getHello",true,L"xmlns",L"http://www.oracle.com");

    // Get the current date/time in a string with this format: 2004-05-19T08:46:04Z
    CkDateTimeW dt;
    dt.SetFromCurrentSystemTime();
    bool bLocal = false;
    const wchar_t *created = dt.getAsTimestamp(bLocal);

    // Now build each UsernameToken block:
    CkXmlW wsse1;
    wsse1.put_Tag(L"wsse:Security");
    wsse1.AddAttribute(L"xmlns:wsse",L"http://schemas.xmlsoap.org/ws/2003/06/secext");
    wsse1.UpdateAttrAt(L"wsse:UsernameToken",true,L"wsu:Id",L"sample");
    wsse1.UpdateAttrAt(L"wsse:UsernameToken",true,L"xmlns:wsu",L"http://schemas.xmlsoap.org/ws/2003/06/utility");
    wsse1.UpdateChildContent(L"wsse:UsernameToken|wsse:Username",L"sample");
    wsse1.UpdateAttrAt(L"wsse:UsernameToken|wsse:Password",true,L"Type",L"wsse:PasswordText");
    wsse1.UpdateChildContent(L"wsse:UsernameToken|wsse:Password",L"oracle");
    wsse1.UpdateChildContent(L"wsse:UsernameToken|wsu:Created",created);

    CkXmlW wsse2;
    wsse2.put_Tag(L"wsse:Security");
    wsse2.AddAttribute(L"soap:actor",L"oracle");
    wsse2.AddAttribute(L"xmlns:wsse",L"http://schemas.xmlsoap.org/ws/2003/06/secext");
    wsse2.UpdateAttrAt(L"wsse:UsernameToken",true,L"wsu:Id",L"oracle");
    wsse2.UpdateAttrAt(L"wsse:UsernameToken",true,L"xmlns:wsu",L"http://schemas.xmlsoap.org/ws/2003/06/utility");
    wsse2.UpdateChildContent(L"wsse:UsernameToken|wsse:Username",L"oracle");
    wsse2.UpdateAttrAt(L"wsse:UsernameToken|wsse:Password",true,L"Type",L"wsse:PasswordText");
    wsse2.UpdateChildContent(L"wsse:UsernameToken|wsse:Password",L"oracle");
    wsse2.UpdateChildContent(L"wsse:UsernameToken|wsu:Created",created);

    // Insert the UsernameToken blocks:
    CkXmlW *xHeader = xml.GetChildWithTag(L"soap:Header");
    xHeader->AddChildTree(wsse1);
    xHeader->AddChildTree(wsse2);
    delete xHeader;

    // Show the XML:
    wprintf(L"%s\n",xml.getXml());
    }