Get user location by IP address

前端 未结 14 1140
深忆病人
深忆病人 2020-11-28 02:23

I have an ASP.NET website written in C#.

On this site I need to automatically show a start page based on the user\'s location.

Can I get name of user\'s city

相关标签:
14条回答
  • 2020-11-28 02:35

    You need an IP-address-based reverse geocoding API... like the one from ipdata.co. I'm sure there are plenty of options available.

    You may want to allow the user to override this, however. For example, they could be on a corporate VPN which makes the IP address look like it's in a different country.

    0 讨论(0)
  • 2020-11-28 02:36

    Following Code work for me.

    Update:

    As I am calling a free API request (json base ) IpStack.

        public static string CityStateCountByIp(string IP)
        {
          //var url = "http://freegeoip.net/json/" + IP;
          //var url = "http://freegeoip.net/json/" + IP;
            string url = "http://api.ipstack.com/" + IP + "?access_key=[KEY]";
            var request = System.Net.WebRequest.Create(url);
            
             using (WebResponse wrs = request.GetResponse())
             using (Stream stream = wrs.GetResponseStream())
             using (StreamReader reader = new StreamReader(stream))
             {
              string json = reader.ReadToEnd();
              var obj = JObject.Parse(json);
                string City = (string)obj["city"];
                string Country = (string)obj["region_name"];                    
                string CountryCode = (string)obj["country_code"];
            
               return (CountryCode + " - " + Country +"," + City);
               }
    
      return "";
    
    }
    

    Edit : First, it was http://freegeoip.net/ now it's https://ipstack.com/ (and maybe now it's a paid service- Free Up to 10,000 request/month)

    0 讨论(0)
  • 2020-11-28 02:36

    you can

    using System.Net;
    using System.IO;
    using Newtonsoft.Json.Linq;
    public ActionResult geoPlugin()
        {
    
            var url = "http://freegeoip.net/json/";
            var request = System.Net.WebRequest.Create(url);
    
            using (WebResponse wrs = request.GetResponse())
            using (Stream stream = wrs.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                string json = reader.ReadToEnd();
                var obj = JObject.Parse(json);
                var City = (string)obj["city"];
                // - For Country = (string)obj["region_name"];                    
                //- For  CountryCode = (string)obj["country_code"];
                Session["CurrentRegionName"]= (string)obj["country_name"];
                Session["CurrentRegion"] = (string)obj["country_code"];
            }
            return RedirectToAction("Index");
        }
    
    0 讨论(0)
  • 2020-11-28 02:41

    I was able to achieve this in ASP.NET MVC using the client IP address and freegeoip.net API. freegeoip.net is free and does not require any license.

    Below is the sample code I used.

    String UserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (string.IsNullOrEmpty(UserIP))
    {
        UserIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }
    string url = "http://freegeoip.net/json/" + UserIP.ToString();
    WebClient client = new WebClient();
    string jsonstring = client.DownloadString(url);
    dynamic dynObj = JsonConvert.DeserializeObject(jsonstring);
    System.Web.HttpContext.Current.Session["UserCountryCode"] = dynObj.country_code;
    

    You can go through this post for more details.Hope it helps!

    0 讨论(0)
  • 2020-11-28 02:46

    IPInfoDB has an API that you can call in order to find a location based on an IP address.

    For "City Precision", you call it like this (you'll need to register to get a free API key):

     http://api.ipinfodb.com/v2/ip_query.php?key=<your_api_key>&ip=74.125.45.100&timezone=false
    

    Here's an example in both VB and C# that shows how to call the API.

    0 讨论(0)
  • 2020-11-28 02:47

    What you need is called a "geo-IP database". Most of them cost some money (albeit not too expensive), especially fairly precise ones. One of the most widely used is MaxMind's database. They have a fairly good free version of IP-to-city database called GeoLity City - it has lots of restrictions, but if you can cope with that that would be probably your best choice, unless you have some money to spare for a subscription to more accurate product.

    And, yeah, they do have a C# API to query geo-IP databases available.

    0 讨论(0)
提交回复
热议问题