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