Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.CkDateTime,
  Chilkat.Xml;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  xml: TXml;
  dt: TCkDateTime;
  bLocal: Boolean;
  created: string;
  wsse1: TXml;
  wsse2: TXml;
  xHeader: TXml;

begin
  //  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 := TXml.Create;
  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
  dt := TCkDateTime.Create;
  dt.SetFromCurrentSystemTime();
  bLocal := False;
  created := dt.GetAsTimestamp(bLocal);

  //  Now build each UsernameToken block:
  wsse1 := TXml.Create;
  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);

  wsse2 := TXml.Create;
  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:
  xHeader := xml.GetChildWithTag('soap:Header');
  xHeader.AddChildTree(wsse1);
  xHeader.AddChildTree(wsse2);
  xHeader.Free;

  //  Show the XML:
  WriteLn(xml.GetXml());


  xml.Free;
  dt.Free;
  wsse1.Free;
  wsse2.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.