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 <C_CkXmlW.h>
#include <C_CkDateTimeW.h>

void ChilkatSample(void)
    {
    HCkXmlW xml;
    HCkDateTimeW dt;
    BOOL bLocal;
    const wchar_t *created;
    HCkXmlW wsse1;
    HCkXmlW wsse2;
    HCkXmlW xHeader;

    // 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:
    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"soap:Envelope");
    CkXmlW_AddAttribute(xml,L"xmlns:soap",L"http://schemas.xmlsoap.org/soap/envelope/");
    CkXmlW_UpdateChildContent(xml,L"soap:Header",L"");
    CkXmlW_UpdateAttrAt(xml,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
    dt = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(dt);
    bLocal = FALSE;
    created = CkDateTimeW_getAsTimestamp(dt,bLocal);

    // Now build each UsernameToken block:
    wsse1 = CkXmlW_Create();
    CkXmlW_putTag(wsse1,L"wsse:Security");
    CkXmlW_AddAttribute(wsse1,L"xmlns:wsse",L"http://schemas.xmlsoap.org/ws/2003/06/secext");
    CkXmlW_UpdateAttrAt(wsse1,L"wsse:UsernameToken",TRUE,L"wsu:Id",L"sample");
    CkXmlW_UpdateAttrAt(wsse1,L"wsse:UsernameToken",TRUE,L"xmlns:wsu",L"http://schemas.xmlsoap.org/ws/2003/06/utility");
    CkXmlW_UpdateChildContent(wsse1,L"wsse:UsernameToken|wsse:Username",L"sample");
    CkXmlW_UpdateAttrAt(wsse1,L"wsse:UsernameToken|wsse:Password",TRUE,L"Type",L"wsse:PasswordText");
    CkXmlW_UpdateChildContent(wsse1,L"wsse:UsernameToken|wsse:Password",L"oracle");
    CkXmlW_UpdateChildContent(wsse1,L"wsse:UsernameToken|wsu:Created",created);

    wsse2 = CkXmlW_Create();
    CkXmlW_putTag(wsse2,L"wsse:Security");
    CkXmlW_AddAttribute(wsse2,L"soap:actor",L"oracle");
    CkXmlW_AddAttribute(wsse2,L"xmlns:wsse",L"http://schemas.xmlsoap.org/ws/2003/06/secext");
    CkXmlW_UpdateAttrAt(wsse2,L"wsse:UsernameToken",TRUE,L"wsu:Id",L"oracle");
    CkXmlW_UpdateAttrAt(wsse2,L"wsse:UsernameToken",TRUE,L"xmlns:wsu",L"http://schemas.xmlsoap.org/ws/2003/06/utility");
    CkXmlW_UpdateChildContent(wsse2,L"wsse:UsernameToken|wsse:Username",L"oracle");
    CkXmlW_UpdateAttrAt(wsse2,L"wsse:UsernameToken|wsse:Password",TRUE,L"Type",L"wsse:PasswordText");
    CkXmlW_UpdateChildContent(wsse2,L"wsse:UsernameToken|wsse:Password",L"oracle");
    CkXmlW_UpdateChildContent(wsse2,L"wsse:UsernameToken|wsu:Created",created);

    // Insert the UsernameToken blocks:
    xHeader = CkXmlW_GetChildWithTag(xml,L"soap:Header");
    CkXmlW_AddChildTree(xHeader,wsse1);
    CkXmlW_AddChildTree(xHeader,wsse2);
    CkXmlW_Dispose(xHeader);

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


    CkXmlW_Dispose(xml);
    CkDateTimeW_Dispose(dt);
    CkXmlW_Dispose(wsse1);
    CkXmlW_Dispose(wsse2);

    }