Client IP using C #

How can I get the IP address of a client machine in C #.? I want to keep a logbook for my online application and save the IP address of the logging system. I want to get the client IP address ....

Thanks for the help...

+3
source share
2 answers
HttpContext.Current.Request.UserHostAddress

This does not attempt to take proxies into account. You can use for this Request.ServerVariables["HTTP_X_FORWARDED_FOR"]. However, make sure that you do not blindly trust this, as it can be faked. It’s better to keep a whitelist of the IP addresses you trust.

+6
source
    String clientIP = 
(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null)?
HttpContext.Current.Request.UserHostAddress:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
+8
source

All Articles