1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.ServiceModel.Dispatcher
Imports System.ServiceModel.Channels
Imports System.Xml
Imports System.IO
Imports System.Xml.XPath
''' <summary>
''' Message inspector injecte un header "Authorization"
''' avec l'erreur 401 et 200 en réponse.
''' </summary>
Public Class CustomProxyHeaderMessageInspector
Implements IClientMessageInspector
Public Sub AfterReceiveReply(ByRef reply As System.ServiceModel.Channels.Message, correlationState As Object) Implements System.ServiceModel.Dispatcher.IClientMessageInspector.AfterReceiveReply
End Sub
Public Function BeforeSendRequest(ByRef request As System.ServiceModel.Channels.Message, channel As System.ServiceModel.IClientChannel) As Object Implements System.ServiceModel.Dispatcher.IClientMessageInspector.BeforeSendRequest
' Ajoute le header avant l'envoi
request.Headers.Add(myheader())
Return request
End Function
Public Function myheader() As System.ServiceModel.Channels.MessageHeader
Dim MyDoc As New XmlDocument()
Try
Dim headerText As String = ""
headerText = "<wsse:UsernameToken xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">" & "<wsse:Username>" & username & " </wsse:Username>" & "<wsse:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"">" & password & "</wsse:Password>" & "</wsse:UsernameToken>"
MyDoc.LoadXml(headerText)
Dim myElement As XmlElement = MyDoc.DocumentElement
Return MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", myElement, False)
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
MyDoc = Nothing
End Try
End Function
End Class |
Partager