Sample code for 30+ languages & platforms
Dart

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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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:
  final xml = CkXml();
  xml.tag = 'soap:Envelope';
  xml.addAttribute('xmlns:soap', 'http://schemas.xmlsoap.org/soap/envelope/');
  xml.updateChildContent('soap:Header', '');
  xml.updateAttrAt('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
  final dt = CkDateTime();
  dt.setFromCurrentSystemTime();
  final bLocal = false;
  final created = dt.getAsTimestamp(bLocal);

  // Now build each UsernameToken block:
  final wsse1 = CkXml();
  wsse1.tag = 'wsse:Security';
  wsse1.addAttribute('xmlns:wsse', 'http://schemas.xmlsoap.org/ws/2003/06/secext');
  wsse1.updateAttrAt('wsse:UsernameToken', true, 'wsu:Id', 'sample');
  wsse1.updateAttrAt('wsse:UsernameToken', true, 'xmlns:wsu', 'http://schemas.xmlsoap.org/ws/2003/06/utility');
  wsse1.updateChildContent('wsse:UsernameToken|wsse:Username', 'sample');
  wsse1.updateAttrAt('wsse:UsernameToken|wsse:Password', true, 'Type', 'wsse:PasswordText');
  wsse1.updateChildContent('wsse:UsernameToken|wsse:Password', 'oracle');
  wsse1.updateChildContent('wsse:UsernameToken|wsu:Created', created);

  final wsse2 = CkXml();
  wsse2.tag = 'wsse:Security';
  wsse2.addAttribute('soap:actor', 'oracle');
  wsse2.addAttribute('xmlns:wsse', 'http://schemas.xmlsoap.org/ws/2003/06/secext');
  wsse2.updateAttrAt('wsse:UsernameToken', true, 'wsu:Id', 'oracle');
  wsse2.updateAttrAt('wsse:UsernameToken', true, 'xmlns:wsu', 'http://schemas.xmlsoap.org/ws/2003/06/utility');
  wsse2.updateChildContent('wsse:UsernameToken|wsse:Username', 'oracle');
  wsse2.updateAttrAt('wsse:UsernameToken|wsse:Password', true, 'Type', 'wsse:PasswordText');
  wsse2.updateChildContent('wsse:UsernameToken|wsse:Password', 'oracle');
  wsse2.updateChildContent('wsse:UsernameToken|wsu:Created', created);

  // Insert the UsernameToken blocks:
  final xHeader = xml.getChildWithTag('soap:Header');
  xHeader.addChildTree(wsse1);
  xHeader.addChildTree(wsse2);

  // Show the XML:
  print(xml.getXml());
}