Client IP using C#

2020-02-01 12:27发布

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

标签: c# asp.net ip
2条回答
何必那么认真
2楼-- · 2020-02-01 12:51
    String clientIP = 
(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null)?
HttpContext.Current.Request.UserHostAddress:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
查看更多
Bombasti
3楼-- · 2020-02-01 12:56
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.

查看更多
登录 后发表回答