Take the 2-minute tour ×

I just started using WCF Services with ASP.NET AJAX. I instantiate my WCF service from Javascript and then pass string variables as arguments to my WCF Service method (with an OperationContract signature). I then return a .NET object (defined with a DataContract) which is bound to my custom Javascript class. I'm having trouble authenticating based on the user logged into my web session. However, the WCF web service is a completely different service with no context to the HttpContext.Current object. What is the most secure way to get access to that object?

share|improve this question
add comment

2 Answers

up vote 24 down vote accepted

You can get access to HttpContext.Current by enabling AspNetCompatibility, preferably via configuration:

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

That in turn allows you to get access to the current user: HttpContext.Current.User - which is what you're after, right?

You can even enforce AspNetCompatibility by decorating your service class with an additional attribute:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

(In the System.ServiceModel.Activation namespace.) If that attribute is in place, your service will fail to start unless AspNetCompatibility is enabled!

share|improve this answer
Thanks for the input. I gave you a point. I'll mark my answer once I have test this out. We are storing the user slightly differently in our application, so we'll have to redesign things as they recommend for forms authentication. Then I can use this approach. –  MacGyver May 19 '11 at 22:05
add comment

You do not have a HttpContext by default but you have many of the same objects present in the OperationContext (which is always present) or the WebOperationContext (which is only available for certain bindings.

You can access the OperationContext or WebOperationContext by using the static .Current property like so: WebOperationContext.Current

share|improve this answer
Thanks for the input, I'll give you a point. –  MacGyver May 19 '11 at 22:05
+1 this should be the accepted answer. We are dealing with WCF which could be hosted in a Windows Service or self-hosted. AspNetCompatilibity is just an old habit. –  Askolein Mar 11 at 10:18
add comment

Your Answer


