Access WCF MessageHeader when OperationContext.Current is null

I have a WCF that is protected with a custom UserNamePasswordValidator. I need to access what will usually be available:

OperationContext.Current.RequestContext.RequestMessage.Headers.To 

so i can parse the url. However, OperationContext.Current is null. Is there a way to get the message header without an OperationContext?

+7
c # wcf
source share
1 answer

Yes, this is possible through Message Inspectors.

OperationContext is not available during UserNamePasswordValidator.Validate , because it will be created later in the pipeline when the call was sent to the corresponding service method.

Typically, you should intercept incoming and outgoing messages early on in the WCF pipeline using Message Inspectors . However, this will not work in your case , as Message Inspectors is called only after successful authentication of the request .

If you need to check your incoming HTTP request before authentication , the only option is to host your WCF service in IIS running in ASP.NET compatibility mode . This way, you can access the request URL through the HttpContext class:

 public override void Validate(string userName, string password) { string url = HttpContext.Current.Request.Url.AbsoluteUri; } 

Related Resources:

+9
source share

All Articles