Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to override the UserHostAddress property of the HttpRequest class?

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.

like image 357
DreamSeeker Avatar asked Aug 08 '12 14:08

DreamSeeker


1 Answers

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;
}
like image 112
PhonicUK Avatar answered Oct 25 '22 13:10

PhonicUK