How to: Add an Id Attribute to a SOAP Header

To sign a custom SOAP header or an element within a custom SOAP header, the element must have an Id attribute.

To add an Id attribute to an XML element

  • Apply an System.Xml.Serialization.XmlAttributeAttribute to the property, field, parameter, or return value that represents the XML element that is being signed or encrypted.

    Set the AttributeName property to Id and the Namespace property to http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd.

    The value of the Id attribute must be a unique identifier within a SOAP message that follows the rules for the xsd:Id type that are defined in the XML Schema specification (http://www.w3.org/2001/XMLSchema). If the SOAP message does not follow the rules for the xsd:Id type, WSE throws an exception with a message text of "Malformed reference".

    The following code example specifies that when a custom SOAP header named OrderTimeHeader is serialized into XML, the value of the Id field is serialized as an XML attribute.

    Note

    Add a using or Imports directive to the System.Xml.Serialization namespace prior to applying an XmlAttribute to the property, field, parameter, or return value.

    Public Class OrderTimeHeader
        Inherits SoapHeader
        ' The following Id field is how you specify a reference.
        <XmlAttributeAttribute("Id", _
             Namespace:="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")> _
        Public Id As String
    
        Public Created As DateTime
        Public Expires As DateTime
        Public Received As DateTime
    
        Public Sub New()
            Id = Guid.NewGuid().ToString()
        End Sub
    End Class
    
    public class OrderTimeHeader : SoapHeader
    {
        // The following Id field is how you specify a reference.
        [XmlAttribute("Id",
             Namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")]
        public String Id;
    
        public DateTime Created;
        public DateTime Expires;
        public DateTime Received;
    
        public OrderTimeHeader()
        {
            Id = Guid.NewGuid().ToString();
        }
    }
    

See Also

Tasks

How to: Digitally Sign a Custom SOAP Header