Custom Output Filter for a C# Web Services SDK Client

The following code fragment shows the custom output filter for the custom security assertion. The custom filter provides three methods:

CustomSecurityClientOutputFilterHok Class constructor that creates token and message signature objects for the SOAP message.
SecureMessageAn override method for the .NET method SendSecurityFilter.SecureMessage. The override method adds the SAML token and message signature to the .NET Security element.
CreateKeyInfoSignatureElementCreates an XML document that specifies the SAML token type and ID.

Output Filter for the Custom SecurityPolicyAssertion

internal class CustomSecurityClientOutputFilterHok : SendSecurityFilter
{
  IssuedToken issuedToken = null;
  string samlAssertionId = null;
  MessageSignature messageSignature = null;

  /// Create a custom SOAP request filter.
  /// (Save the token and certificate.)
  public CustomSecurityClientOutputFilterHok(CustomSecurityAssertionHok parentAssertion)
         : base(parentAssertion.ServiceActor, true)
  {
    issuedToken = new IssuedToken(parentAssertion.BinaryToken, parentAssertion.TokenType);
    samlAssertionId = parentAssertion.BinaryToken.Attributes.GetNamedItem("ID").Value;
    messageSignature = new MessageSignature(parentAssertion.SecurityToken);
  }

  ///  Secure the SOAP message before its sent to the server.
  public override void SecureMessage(SoapEnvelope envelope, Security security)
  {
    //create KeyInfo XML element
    messageSignature.KeyInfo = new KeyInfo();
    messageSignature.KeyInfo.LoadXml(CreateKeyInfoSignatureElement());

    security.Tokens.Add(issuedToken);
    security.Elements.Add(messageSignature);
  }

  /// Helper method to create a custom key info signature element.
  /// Returns Key info XML element.
  private XmlElement CreateKeyInfoSignatureElement()
  {
    var xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(@"<root><SecurityTokenReference 
    xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" 
    xmlns:wsse=""http://docs.oasis-open.org/wss/oasis-wss-wssecurity-secext-1.1.xsd"" 
    wsse:TokenType=""http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0"">
    <KeyIdentifier
    xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" 
    ValueType=""http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLID"">"
    + samlAssertionId + @"</KeyIdentifier></SecurityTokenReference></root>");
    return xmlDocument.DocumentElement;
  }