Signing a Custom SOAP Header using WCF


Mike Logan

I'm trying to write a simple client to prove that this works. I need to be
able to send a web service request (SOAP 1.1/HTTP) with a signed custom soap
header. I'm able to add the custom soap header, I just can't get it signed.
I can find the cert I want to use, it just doesn't sign the custom soap

Here are the important parts of my code.

Custom Message Inspector

Public Class MyCustomMessageInspector
Implements IClientMessageInspector

Public Sub AfterReceiveReply(ByRef reply As
System.ServiceModel.Channels.Message, ByVal correlationState As Object)
End Sub

Public Function BeforeSendRequest(ByRef request As
System.ServiceModel.Channels.Message, ByVal channel As
System.ServiceModel.IClientChannel) As Object Implements
request.Headers.Add(New CustomABCSoapHeader("heliolo"))
Return request
End Function
End Class

Here is my custom message header class.

Public Class CustomABCSoapHeader
Inherits MessageHeader

Dim val As String

Protected Overrides Sub OnWriteHeaderContents(ByVal writer As
Xml.XmlDictionaryWriter, ByVal messageVersion As MessageVersion)
writer.WriteAttributeString("name", CustomABC)
End Sub 'OnWriteHeaderContents

Public Sub New(ByVal value As String)
Me.CustomABC = value
End Sub

<MessageHeader(ProtectionLevel:=System.Net.Security.ProtectionLevel.Sign)> _
Public Property CustomABC() As String
Return val
End Get
Set(ByVal value As String)
val = value
End Set
End Property

Public Overrides ReadOnly Property Name() As String
Return "CustomABC"
End Get
End Property

Public Overrides ReadOnly Property [Namespace]() As String
Return ""
End Get
End Property
End Class 'MyMessageHeader

Here is my Custom Endpoint Behavior.

Public Class MyCustomBehavior
Implements IEndpointBehavior

Public Sub AddBindingParameters(ByVal endpoint As
System.ServiceModel.Description.ServiceEndpoint, ByVal bindingParameters As
System.ServiceModel.Channels.BindingParameterCollection) Implements

End Sub

Public Sub ApplyClientBehavior(ByVal endpoint As
System.ServiceModel.Description.ServiceEndpoint, ByVal clientRuntime As
System.ServiceModel.Dispatcher.ClientRuntime) Implements
clientRuntime.MessageInspectors.Add(New MyCustomMessageInspector)
End Sub

Public Sub ApplyDispatchBehavior(ByVal endpoint As
System.ServiceModel.Description.ServiceEndpoint, ByVal endpointDispatcher As
System.ServiceModel.Dispatcher.EndpointDispatcher) Implements

End Sub

Public Sub Validate(ByVal endpoint As
System.ServiceModel.Description.ServiceEndpoint) Implements

End Sub
End Class

And finally here is my client code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.Windows.RoutedEventArgs) Handles Button1.Click

Dim myws As New MyWS.MyWSSoapClient


myws.ChannelFactory.Credentials.ClientCertificate.SetCertificate( _
"cn=customcertnamehere", _

System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, _

myws.ChannelFactory.Endpoint.Behaviors.Add(New MyCustomBehavior())

TextBox1.Text = myws.WSOperation1("Hello world...sign me please.")

Catch ex As Exception

MsgBox(ex.ToString, MsgBoxStyle.OkOnly, "error")

End Try

End Sub

Thanks for any help you can provide.

Steven Cheng

Hi Mike,

Regarding on this WCF custom header extension issue, I've found your
another thread in this newsgroup and have replied you there:

Subject: Sign custom SOAP Header on outbound call with WCF
Newsgroups: microsoft.public.dotnet.framework

Please feel free to have a look and followup in tha thread.


Steven Cheng

Microsoft MSDN Online Support Lead

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question