我想获取在我的网站上注册的人的 ip 地址。如何在 ASPNET 中执行此操作。我使用了以下代码,但是它没有获得正确的 IP 地址
string ipaddress = Request.UserHostAddress;
最佳答案
您可以使用此方法获取客户端机器的IP地址。
public static String GetIP()
{
String ip =
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return ip;
}
关于c# - 如何获取IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1907195/