Question

I have a situation where I need to put my application behind a proxy server, this causes all the request's that are coming to my application to have the same set of IP addresses used by the proxy servers. However the Proxy server provides the real IP address of the requestor in a custom header, that I can use through my application so I can know the real IP address of the requestor. This is mainly used for logging and tracking. Is there a way I can have the UserHostAddress property return the value from this custom header? This would save a lot of work, because this property referenced about a few hundred time.

Was it helpful?

Solution

It's not possible to change the behavior of the UserHostAddress property, however what you can do is add an extension method to the Request class (something like GetRealUserHostAddress()) and just do a global replace on UserHostAddress -> GetRealUserHostAddress() to rapidly sort out all the instances of it in your solution.

public static string GetRealUserHostAddress(this HttpRequestBase request)
{
    return request.Headers["HeaderName"] ?? request.UserHostAddress;
}

OTHER TIPS

If you are saying that the proxy returns the real ip address of the client making the request, you don't need to use the UserHostAddress to read it; you can simply read the header directly:

string realIP = HttpContext.Request.Headers["actual_header_key"];

No, it's not possible. You can read the custom header and place in the request context and use that later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top