Manipulating the HTTP header in WCF before HTTP authentication in HttpBinding
-
03-07-2019 - |
Question
This is in relation to this question
I am hosting this WCF service in a custom service host. IIS is not an option.
Is there a way to inject an HTTP "Authenticate" header in the server stack (if it's missing) early on in the request process - such that a default user can be passed into the authentication/authorization process that I have in place for anonymous requests?
I need the SERVER to inject this on the receiving end - before further WCF processing...
I'm pretty sure I can do this with a custom BindingElement, or by extending the WebHttpBinding, but any examples or guidance would be greatly appreciated. I can't find any examples of early header manipulation (pre authentication) in WCF.
Solution
By implementing an IDispatchMessageInspector you can plug into the pipeline server side; and manipulate messages.
In the AfterReceiveRequest method you can add to Request.Headers quite happily.
Wrap it with a behaviour attribute and apply it to your service.
OTHER TIPS
I would type this out, but its a lot. So here is a good reference.
Avner Kashtan's blog post outlines what I "think" you're looking for. Hopefully this helps!