简体   繁体   中英

Client IP using C#

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...

    String clientIP = 
(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null)?
HttpContext.Current.Request.UserHostAddress:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM