Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access HttpContext.Current from WCF Web Service

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?

like image 879
MacGyver Avatar asked May 05 '11 21:05

MacGyver


People also ask

How do I find HttpContext current?

If you're writing custom middleware for the ASP.NET Core pipeline, the current request's HttpContext is passed into your Invoke method automatically: public Task Invoke(HttpContext context) { // Do something with the current HTTP context... }

What is HttpContext current in C#?

The property stores the HttpContext instance that applies to the current request. The properties of this instance are the non-static properties of the HttpContext class. You can also use the Page. Context property to access the HttpContext object for the current HTTP request.


3 Answers

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

<configuration>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
</configuration>

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!

like image 177
mthierba Avatar answered Oct 06 '22 09:10

mthierba


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

like image 27
faester Avatar answered Oct 06 '22 09:10

faester


In case you don't want to change Web.config or you cannot change it:

private string GetClientIPAddress()
        {
            var props = OperationContext.Current.IncomingMessageProperties;
            var endpointProperty = props[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            if (endpointProperty != null)
            {
                if (endpointProperty.Address == "::1" || String.IsNullOrEmpty(endpointProperty.Address))
                    return "127.0.0.1";

                return endpointProperty.Address;
            }

            return String.Empty;
        }
like image 38
nikolai.serdiuk Avatar answered Oct 06 '22 08:10

nikolai.serdiuk