23. November 2010 15:48
/
Vitaly Zayko
/
/
Comments (0)
I was looking for a snippet to add a custom header to WCF service request. Why? For example, to add authentication header to add Basic Authentication support. (Yes – WCF supports Basic Authorization by itself but didn’t find how to use it without SSL).
Here is the shortest way.
Use this snippet on your client side:
using (MyServ.ServiceClient proxy = new MyServ.ServiceClient ())
{
using (new System.ServiceModel.OperationContextScope (proxy.InnerChannel))
{
MessageHeader head = MessageHeader .CreateHeader("Authorization" , "http://mynamespace.com/v1" , data);
OperationContext .Current.OutgoingMessageHeaders.Add(head);
}
}
And just in case, here is how to retrieve this information from Http header on the server side:
string auth = OperationContext .Current.
IncomingMessageHeaders.
GetHeader<string >("Authorization" , "http://mynamespace.com/v1" );
More about OperationContextScope Class here.
7d745302-c6c7-471e-a8b0-1b2a0adb569b|1|5.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04