How can i get IP address of client machine in C#.?
I want to keep a log register for my online application and to keep IP address of logging system i want to get the IP address of client….
Advance Thanks…
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
String clientIP = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null)? HttpContext.Current.Request.UserHostAddress: HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Method 2
HttpContext.Current.Request.UserHostAddress
This doesn’t attempt to take into account proxies. For that, you can use Request.ServerVariables["HTTP_X_FORWARDED_FOR"]. However, make sure you’re not trusting that blindly, since it could be forged. It’s better to keep a whitelist of IPs for which you trust it.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0