如何通过Asp.Net获取您网站的每个网站的ipaddress和位置?
由于
答案 0 :(得分:4)
获取用户的IP使用权:
Request.UserHostAddress
您可以使用此网络服务获取其地理位置。 http://iplocationtools.com/ip_location_api.php
答案 1 :(得分:3)
string VisitorIPAddress = Request.UserHostAddress.ToString();
根据ipaddress,您可以缩小位置范围:find the geographical location of a host
答案 2 :(得分:1)
如果您在代理后面,则Request.UserHostAddress将不起作用。使用此代码:
public static String GetIPAddress()
{
String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
else
ip = ip.Split(',')[0];
return ip;
}
请注意,应该使用HTTP_X_FORWARDED_FOR,因为它可以返回由逗号分隔的多个IP地址,您需要使用Split功能。有关详细信息,请参阅this page。
答案 3 :(得分:0)
以下属性应该为您提供客户端(或客户端代理服务器)的IP地址
Request.UserHostAddress
至于位置,您需要使用一些像MaxMind这样的GeoIP / GeoLocation插件来解决这个问题。
答案 4 :(得分:0)
要获取IP,请执行以下操作:
Request.UserHostAddress
您可以使用webservice(较慢)或数据库(更快)将IP映射到位置,如下所示: http://ip-to-country.webhosting.info/node/view/5
答案 5 :(得分:0)
这是与服务器技术无关的,但我建议在Google的AJAX加载器上进行背负式支持:http://code.google.com/apis/ajax/documentation/#ClientLocation
它是在Javascript中,甚至会给你这个人的城市/州/国家(好吧,它根据IP地址进行猜测)。将其发布回服务器,它可以在ASP.NET或其他任何地方使用。