Add custom header into Security element with WCF
Can I add and read a custom header in t开发者_C百科he Envelope/Header/Security element? I tried using the MessageHeader attribute, but that does not allow me to put the header in the Security element.
I created a class that implements IClientMessageInspector thinking that I could access the Security header like so:
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
request = buffer.CreateMessage();
Message originalMessage = buffer.CreateMessage();
foreach (MessageHeader h in originalMessage.Headers)
{
Console.WriteLine("\n{0}\n", h);
}
return null;
}
But the Security header is not present in the originalMessage.Headers object.
Create a custom message encoder: http://msdn.microsoft.com/en-us/library/ms751486.aspx.
You can access the message headers in your encoder's WriteMessage override. Note that the Message's Headers property will not contain the Security header (though this may depend on the type of security you're using). Write out the message to a stream or file using, say, Message.WriteMessage(XmlWriter). The stream/file will contain the contents of the message just before being sent over the wire, including the Security element. From there, you can modify your message as necessary and return an ArraySegment including your changes.
精彩评论