Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkXml.h>
#include <C_CkDateTime.h>

void ChilkatSample(void)
    {
    HCkXml xml;
    HCkDateTime dt;
    BOOL bLocal;
    const char *created;
    HCkXml wsse1;
    HCkXml wsse2;
    HCkXml 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 = CkXml_Create();
    CkXml_putTag(xml,"soap:Envelope");
    CkXml_AddAttribute(xml,"xmlns:soap","http://schemas.xmlsoap.org/soap/envelope/");
    CkXml_UpdateChildContent(xml,"soap:Header","");
    CkXml_UpdateAttrAt(xml,"soap:Body|getHello",TRUE,"xmlns","http://www.oracle.com");

    // Get the current date/time in a string with this format: 2004-05-19T08:46:04Z
    dt = CkDateTime_Create();
    CkDateTime_SetFromCurrentSystemTime(dt);
    bLocal = FALSE;
    created = CkDateTime_getAsTimestamp(dt,bLocal);

    // Now build each UsernameToken block:
    wsse1 = CkXml_Create();
    CkXml_putTag(wsse1,"wsse:Security");
    CkXml_AddAttribute(wsse1,"xmlns:wsse","http://schemas.xmlsoap.org/ws/2003/06/secext");
    CkXml_UpdateAttrAt(wsse1,"wsse:UsernameToken",TRUE,"wsu:Id","sample");
    CkXml_UpdateAttrAt(wsse1,"wsse:UsernameToken",TRUE,"xmlns:wsu","http://schemas.xmlsoap.org/ws/2003/06/utility");
    CkXml_UpdateChildContent(wsse1,"wsse:UsernameToken|wsse:Username","sample");
    CkXml_UpdateAttrAt(wsse1,"wsse:UsernameToken|wsse:Password",TRUE,"Type","wsse:PasswordText");
    CkXml_UpdateChildContent(wsse1,"wsse:UsernameToken|wsse:Password","oracle");
    CkXml_UpdateChildContent(wsse1,"wsse:UsernameToken|wsu:Created",created);

    wsse2 = CkXml_Create();
    CkXml_putTag(wsse2,"wsse:Security");
    CkXml_AddAttribute(wsse2,"soap:actor","oracle");
    CkXml_AddAttribute(wsse2,"xmlns:wsse","http://schemas.xmlsoap.org/ws/2003/06/secext");
    CkXml_UpdateAttrAt(wsse2,"wsse:UsernameToken",TRUE,"wsu:Id","oracle");
    CkXml_UpdateAttrAt(wsse2,"wsse:UsernameToken",TRUE,"xmlns:wsu","http://schemas.xmlsoap.org/ws/2003/06/utility");
    CkXml_UpdateChildContent(wsse2,"wsse:UsernameToken|wsse:Username","oracle");
    CkXml_UpdateAttrAt(wsse2,"wsse:UsernameToken|wsse:Password",TRUE,"Type","wsse:PasswordText");
    CkXml_UpdateChildContent(wsse2,"wsse:UsernameToken|wsse:Password","oracle");
    CkXml_UpdateChildContent(wsse2,"wsse:UsernameToken|wsu:Created",created);

    // Insert the UsernameToken blocks:
    xHeader = CkXml_GetChildWithTag(xml,"soap:Header");
    CkXml_AddChildTree(xHeader,wsse1);
    CkXml_AddChildTree(xHeader,wsse2);
    CkXml_Dispose(xHeader);

    // Show the XML:
    printf("%s\n",CkXml_getXml(xml));


    CkXml_Dispose(xml);
    CkDateTime_Dispose(dt);
    CkXml_Dispose(wsse1);
    CkXml_Dispose(wsse2);

    }