Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL oXml
LOCAL oDt
LOCAL nBLocal
LOCAL cCreated
LOCAL oWsse1
LOCAL oWsse2
LOCAL oXHeader

//  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:
oXml := CreateObject("Chilkat.Xml")
oXml:Tag := "soap:Envelope"
oXml:AddAttribute("xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/")
oXml:UpdateChildContent("soap:Header", "")
oXml:UpdateAttrAt("soap:Body|getHello", 1, "xmlns", "http://www.oracle.com")

//  Get the current date/time in a string with this format: 2004-05-19T08:46:04Z
oDt := CreateObject("Chilkat.CkDateTime")
oDt:SetFromCurrentSystemTime()
nBLocal := 0
cCreated := oDt:GetAsTimestamp(nBLocal)

//  Now build each UsernameToken block:
oWsse1 := CreateObject("Chilkat.Xml")
oWsse1:Tag := "wsse:Security"
oWsse1:AddAttribute("xmlns:wsse", "http://schemas.xmlsoap.org/ws/2003/06/secext")
oWsse1:UpdateAttrAt("wsse:UsernameToken", 1, "wsu:Id", "sample")
oWsse1:UpdateAttrAt("wsse:UsernameToken", 1, "xmlns:wsu", "http://schemas.xmlsoap.org/ws/2003/06/utility")
oWsse1:UpdateChildContent("wsse:UsernameToken|wsse:Username", "sample")
oWsse1:UpdateAttrAt("wsse:UsernameToken|wsse:Password", 1, "Type", "wsse:PasswordText")
oWsse1:UpdateChildContent("wsse:UsernameToken|wsse:Password", "oracle")
oWsse1:UpdateChildContent("wsse:UsernameToken|wsu:Created", cCreated)

oWsse2 := CreateObject("Chilkat.Xml")
oWsse2:Tag := "wsse:Security"
oWsse2:AddAttribute("soap:actor", "oracle")
oWsse2:AddAttribute("xmlns:wsse", "http://schemas.xmlsoap.org/ws/2003/06/secext")
oWsse2:UpdateAttrAt("wsse:UsernameToken", 1, "wsu:Id", "oracle")
oWsse2:UpdateAttrAt("wsse:UsernameToken", 1, "xmlns:wsu", "http://schemas.xmlsoap.org/ws/2003/06/utility")
oWsse2:UpdateChildContent("wsse:UsernameToken|wsse:Username", "oracle")
oWsse2:UpdateAttrAt("wsse:UsernameToken|wsse:Password", 1, "Type", "wsse:PasswordText")
oWsse2:UpdateChildContent("wsse:UsernameToken|wsse:Password", "oracle")
oWsse2:UpdateChildContent("wsse:UsernameToken|wsu:Created", cCreated)

//  Insert the UsernameToken blocks:
oXHeader := oXml:GetChildWithTag("soap:Header")
oXHeader:AddChildTree(oWsse1)
oXHeader:AddChildTree(oWsse2)
oXHeader:destroy()

//  Show the XML:
? oXml:GetXml()

oXml:destroy()
oDt:destroy()
oWsse1:destroy()
oWsse2:destroy()