How to get the geographic location using IP Address in asp.net mvc
Asked Answered
D

3

6

I know that this code:

string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress = Request.ServerVariables["REMOTE_ADDR"];
        }

will give me the IP Address of the user but I don't know how to get the location of the user

I want to know the location and related information on basis of IP Address.

Deemphasize answered 8/8, 2014 at 12:20 Comment(4)
What you're looking for is called "geolocation". A Google search for something like "HTML geolocation" yields a lot of results, for example: developer.mozilla.org/en-US/docs/Web/API/Geolocation/… Conversely, if you're looking to geolocate a given IP address, you might try something like "geocoding" for which there are a number of APIs, for example: developers.google.com/maps/documentation/geocodingCricket
codeproject.com/Articles/185508/IP-Address-to-Geolocation here is the what you needBlah
Note: IP Address is usually enough to narrow a user down to a particular country, but much more than that is fuzzy. An IP Address location will resolve to the ISP node it's belongs to, which may be a particular city if it's a big one, like New York, but in more suburban or rural areas may only resolve to the state the user is in or even just a region like midwest U.S. To clarify, what I'm talking about is resolution to the user's actual location. You'll get full city, state, etc., but that's for the node not the user.Razzia
Also VPN and proxy services can mask the user's true IP, and hence their location. A U.S. user could present as one from China if they use a Chinese proxy. In other words, don't rely on this information for anything important. If you need the user's location for a real purpose, ask them for it.Razzia
V
8

This should help you - http://freegeoip.net/

freegeoip.net is a public REST API for searching geolocation of IP addresses and host names. Send HTTP GET requests to: freegeoip.net/{format}/{ip_or_hostname}. The API supports both HTTP and HTTPS and Supported formats are csv, xml or json.

Vicenary answered 8/8, 2014 at 13:56 Comment(0)
L
4

Model to bind the user location details

public class IpInfo
{
    [JsonProperty("ip")]
    public string Ip { get; set; }


    [JsonProperty("city")]
    public string City { get; set; }

    [JsonProperty("region_name")]
    public string Region { get; set; }

    [JsonProperty("country_name")]
    public string Country { get; set; }

    [JsonProperty("time_zone")]
    public string TimeZone { get; set; }


    [JsonProperty("longitude")]
    public string Longitude { get; set; }

    [JsonProperty("latitude")]
    public string Latitude { get; set; }      
}

Function

private  IpInfo GetUserLocationDetailsyByIp(string ip)
    {
        var ipInfo = new IpInfo();
        try
        {
            var info = new WebClient().DownloadString("http://freegeoip.net/json/" + ip);
            ipInfo = JsonConvert.DeserializeObject<IpInfo>(info);
        }
        catch (Exception ex)
        {
            //Exception Handling
        }

        return ipInfo;
    }

Calling function with IP value

var ipDetails = GetUserCountryByIp("8.8.8.8"); //IP value
Landfall answered 15/3, 2017 at 13:56 Comment(0)
M
1

This answer for people is searching in 2022 :).

You can use ip3country-dotnet. It is small library <500KB. This library is using ip2location the lite version, it is totally free!.

Way of usage: Install IP3Country, then write this code

// Lookup using ip4 str
var country = CountryLookup.LookupIPStr("123.45.67.8"); // => 'KR'

// Lookup using numeric ip
country = CountryLookup.LookupIPNumber(2066563848); // => 'KR'
Marivaux answered 8/12, 2022 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.