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.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With