C# 获取访问您网站的每个用户的 IP 地址和位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:37:20  来源:igfitidea点击:

Getting ipaddress and location of every user visiting your website

c#asp.netjavascript

提问by Josh

How do you get the ipaddress and location of every website vistor of your website through Asp.Net?

你如何通过Asp.Net获取你网站的每个网站访问者的IP地址和位置?

Thanks

谢谢

采纳答案by Nathan Koop

To get the user's IP use:

要获取用户的 IP 使用:

Request.UserHostAddress

You can use this webservice to get their geographic location. http://iplocationtools.com/ip_location_api.php

您可以使用此网络服务来获取他们的地理位置。 http://iplocationtools.com/ip_location_api.php

回答by TStamper

 string VisitorIPAddress = Request.UserHostAddress.ToString();

and based on the ipaddress you can narrow down the location: find the geographical location of a host

并根据ipaddress缩小位置:找到主机的地理位置

回答by Eoin Campbell

Well the following property should give you the IP Address of teh client (or the clients proxy server)

那么以下属性应该为您提供客户端(或客户端代理服务器)的 IP 地址

Request.UserHostAddress

As for location, you'd need to use some GeoIP/GeoLocation plugin like MaxMind to figure that out.

至于位置,你需要使用一些像 MaxMind 这样的 GeoIP/GeoLocation 插件来解决这个问题。

http://www.maxmind.com/

http://www.maxmind.com/

回答by adamJLev

To get the IP do:

要获取 IP,请执行以下操作:

Request.UserHostAddress

And you can map IP to location using a webservice (slower) or a database (faster) like this: http://ip-to-country.webhosting.info/node/view/5

您可以使用网络服务(较慢)或数据库(较快)将 IP 映射到位置,如下所示:http: //ip-to-country.webhosting.info/node/view/5

回答by Tom Lianza

It's server technology-agnostic, but I'd recommend piggy-backing on Google's AJAX loader: http://code.google.com/apis/ajax/documentation/#ClientLocation

它与服务器技术无关,但我建议在 Google 的 AJAX 加载器上搭载:http: //code.google.com/apis/ajax/documentation/#ClientLocation

It's in Javascript and will even give you the person's city/state/country (well, it takes a guess based on IP address). Post it back to the server and it's available to you in ASP.NET or whatever.

它是在 Javascript 中,甚至会给你这个人的城市/州/国家(嗯,它需要根据 IP 地址猜测)。将它发送回服务器,您就可以在 ASP.NET 或其他任何地方使用它。

回答by Anthony

Request.UserHostAddress won't work if you're behind a proxy. Use this code:

如果您在代理后面,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;
}

Note that HTTP_X_FORWARDED_FOR should be used BUT as it can return multiple IP addresses separated by a comma you need to use the Split function. See this pagefor more info.

请注意,应该使用 HTTP_X_FORWARDED_FOR,因为它可以返回多个 IP 地址,用逗号分隔,您需要使用 Split 函数。有关更多信息,请参阅此页面