Signing a Custom SOAP Header using WCF

  • Thread starter Thread starter Mike Logan
  • Start date Start date
M

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
header.

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)
Implements
System.ServiceModel.Dispatcher.IClientMessageInspector.AfterReceiveReply
'nothing
End Sub

Public Function BeforeSendRequest(ByRef request As
System.ServiceModel.Channels.Message, ByVal channel As
System.ServiceModel.IClientChannel) As Object Implements
System.ServiceModel.Dispatcher.IClientMessageInspector.BeforeSendRequest
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
Get
Return val
End Get
Set(ByVal value As String)
val = value
End Set
End Property

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

Public Overrides ReadOnly Property [Namespace]() As String
Get
Return "http://abc.com"
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
System.ServiceModel.Description.IEndpointBehavior.AddBindingParameters

End Sub

Public Sub ApplyClientBehavior(ByVal endpoint As
System.ServiceModel.Description.ServiceEndpoint, ByVal clientRuntime As
System.ServiceModel.Dispatcher.ClientRuntime) Implements
System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior
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
System.ServiceModel.Description.IEndpointBehavior.ApplyDispatchBehavior

End Sub

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

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

Try



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

System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, _
System.Security.Cryptography.X509Certificates.StoreName.My)

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.
 
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.

Sincerely,

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).
 
Back
Top