hmk hmk - 1 month ago 20
ASP.NET (C#) Question

using ASP.NET to display the visitor location like country, state and city in master page

How to get visitor location like country, state and city using ASP.NET, i saw some examples All are based on IP Address to get the Details(based on some free services), now my problem is above example that services only working registered Ip addresses, but now my task is when user browse the my asp.net website, display the Country, state and city how it possible, please give me any suggestion, urgent

Thank u
hemanth

Answer

You can use a WebClient request to something like ipinfodb, if you register (free) for an api key:

var client = new System.Net.WebClient();
var ip = Request.UserHostAddress;
var url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}", apiKey, ip);
var info = client.DownloadString(url).ToString();

Then parse and display the info in your page.

There are many IP locator APIs, whichever one you use, the general idea is the same, but you will parse the info result differently depending on the service.

But walther is correct, getting location info from IP addresses isn't always reliable.