tags:

views:

75

answers:

1

I have a WCF that is being secured using a custom UserNamePasswordValidator. I need to access what normally would be available in:

OperationContext.Current.RequestContext.RequestMessage.Headers.To

so I can parse the URL. However, OperationContext.Current is null. Is there a way to get at the message header without OperationContext?

+1  A: 

Yes, it is possible through Message Inspectors.

The OperationContext is not available during the UserNamePasswordValidator.Validate method because it will be created later in the pipeline, when the call has been dispatched to the appropriate service method.

Normally you would intercept incoming and outgoing messages early in the WCF pipeline by using Message Inspectors. However this won't work in your case, since Message Inspectors are invoked only after the request has successfully been authenticated.

If you need to inspect the incoming HTTP request before authentication, your only option is to host your WCF service in IIS running in ASP.NET Compatibility Mode. This way you'll be able to access the request's URL through the HttpContext class:

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

Related resources:

Enrico Campidoglio
Did a quick test and hosted a service in ASP.NET Compatibility Mode on my local IIS and HttpContext.Current was still null. Looks like I'll have to do Authentication in each service call :(
Mark Boltuc
Looks like I can't do the Authentication in each service call... There is no native support for getting the user's password outside the UserNamePasswordValidator.Validate method.
Mark Boltuc
Upgraded the service from .NET 3.5 to 4.0 and your solution worked. Thanks!
Mark Boltuc
Glad I could help :-)
Enrico Campidoglio