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, so all the request coming into my application should have the same set of IP addresses that are used by proxy servers. However, the proxy server provides the real IP address of the requestor in a custom header, which I can use through my application to find out the real IP address of the requestor. It is mainly used for registration and tracking. Is there a way for the UserHostAddress property to return the value from this custom header? This will save a lot of work because this property is referenced several hundred times.

+4
source share
3 answers

It is 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 make a global replacement with UserHostAddress → GetRealUserHostAddress () to quickly sort all instances in your solution .

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

If you say that the proxy returns the real IP address of the client making the request, you do not need to use UserHostAddress to read it; you can just read the header directly:

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

No, It is Immpossible. You can read the custom header and put it in the request context and use it later.

0
source

All Articles