Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 340
Sumit Kesarwani Avatar asked Aug 08 '14 12:08

Sumit Kesarwani


People also ask

Can you find a geographical location with an IP address?

IP geolocation is the mapping of an IP address to the geographic location of the internet from the connected device. By geographically mapping the IP address, it provides you with location information such as the country, state, city, zip code, latitude/longitude, ISP, area code, and other information.

How can I find the location of an IP address?

Just send HTTP GET request to https://api.ipgeolocationapi.com with different parameters and get the JSON result. For example, the following HTTP GET request will give you a location of the specified IP address. The following request returns the country information specified by the ISO country code.

How can get current location in ASP NET MVC?

getCurrentPosition(showPosition, showError); to get the current positions. It is an HTML5 API. See in this above code sections first we are getting latitude and longitude by using the following code: position.


3 Answers

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.

like image 80
Rabi Avatar answered Nov 15 '22 06:11

Rabi


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
like image 36
Shemeemsha R A Avatar answered Nov 15 '22 05:11

Shemeemsha R A


To get the geolocation of the user using their IP address you can try IPinfo.io. They have a C# library that you can use.

// namespace
using IPinfo;
using IPinfo.Models;

// making API call
string ip = "216.239.36.21";
IPResponse ipResponse = await client.IPApi.GetDetailsAsync(ip);

// accessing location details from response
Console.WriteLine($"IPResponse.IP: {ipResponse.IP}");
Console.WriteLine($"IPResponse.City: {ipResponse.City}");
Console.WriteLine($"IPResponse.Company.Name: {ipResponse.Company.Name}");
Console.WriteLine($"IPResponse.Country: {ipResponse.Country}");
Console.WriteLine($"IPResponse.CountryName: {ipResponse.CountryName}");

You can even use their REST API. Documentation.

like image 20
anyfactor Avatar answered Nov 15 '22 05:11

anyfactor