I want to implement this in ASP.NET. I don't have any idea about how to do so, unfortunately.
-
1You can only get an approximate location, and it may be incorrect. – John Saunders Sep 11 '09 at 05:21
-
Same. Sorry for the concurrent edit! Feel free to rollback over me. – JoshJordan Sep 11 '09 at 05:24
8 Answers
Here how it is done in asp.net
Request.ServerVariables("REMOTE_ADDR")
Get a copy of IP adress database by location here

- 15,207
- 15
- 92
- 123

- 3,410
- 1
- 21
- 32
Why not use Google Analytics? You will get more than what you need. Alternatively you can get the client's ip and use service like ip2location to get the location.
Check this similar question as well. finding clients location in asp.net page.
by using
string userHost = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (String.IsNullOrEmpty(userHost) ||
String.Compare(userHost, "unknown", true) == 0)
{
userHost = Request.UserHostAddress;
}
you will get users ip address . Based on this ip address you can find out visitor location details by calling some webservice .

- 160,644
- 26
- 247
- 397

- 11,279
- 2
- 34
- 47
-
That server variable is only available if your webserver is behind a proxy or some kind of network device. – David Feb 10 '10 at 14:43
-
1I believe that was the whole point of his logic, to check for that, and then fall back on the user host address if it doesn't exist. – Justin Mar 18 '14 at 23:23
There are a few free web services out there which provide IP-to-location services. For instance:
http://freegeoip.net/
http://www.hostip.info/
http://ipinfodb.com/ip_location_api.php

- 4,473
- 2
- 29
- 35
IPAddressExtensions is a free codeplex class library if all you just want is the Country the IP is located from.

- 84,693
- 113
- 396
- 647
First, get the IP address of the visitor using Request.ServerVariables("REMOTE_ADDR"). Bear in mind that the visitor could be using a proxy server in which case the IP address may not be their actual IP address. For the proxy case, you can check if Request.ServerVariables("HTTP_X_FORWARDED_FOR") contains a value. This will be the actual IP address if the proxy server is not an anonymous proxy server.
Then you have 2 options, using a web service or querying data from your own database. Either way, you will need data which can match an IP address of the visitor to their country, state and city.

- 170,088
- 45
- 397
- 571
Using the geolocation API at ipStack (https://ipstack.com), I've used the following:
<script type="text/javascript"> $(document).ready(function() { $.ajax({ 'url': 'https://www.freegeoip.net/json/@(HttpContext.Current.Request.UserHostAddress)', 'type': 'GET', 'success': function(data) { // for example if (data.country_code === "GB") { ... further logic here } } }); }); </script>
It returns a lot of useful information, e.g.:
{
"ip": "134.201.250.155",
"hostname": "134.201.250.155",
"type": "ipv4",
"continent_code": "NA",
"continent_name": "North America",
"country_code": "US",
"country_name": "United States",
"region_code": "CA",
"region_name": "California",
"city": "Los Angeles",
"zip": "90013",
"latitude": 34.0453,
"longitude": -118.2413,
"location": {
"geoname_id": 5368361,
"capital": "Washington D.C.",
"languages": [
{
"code": "en",
"name": "English",
"native": "English"
}
],
"country_flag": "https://assets.ipstack.com/images/assets/flags_svg/us.svg",
"country_flag_emoji": "",
"country_flag_emoji_unicode": "U+1F1FA U+1F1F8",
"calling_code": "1",
"is_eu": false
},
"time_zone": {
"id": "America/Los_Angeles",
"current_time": "2018-03-29T07:35:08-07:00",
"gmt_offset": -25200,
"code": "PDT",
"is_daylight_saving": true
},
"currency": {
"code": "USD",
"name": "US Dollar",
"plural": "US dollars",
"symbol": "$",
"symbol_native": "$"
},
"connection": {
"asn": 25876,
"isp": "Los Angeles Department of Water & Power"
},
"security": {
"is_proxy": false,
"proxy_type": null,
"is_crawler": false,
"crawler_name": null,
"crawler_type": null,
"is_tor": false,
"threat_level": "low",
"threat_types": null
}
}

- 18,176
- 3
- 37
- 51
Get the client IP and find the location of IP using any IP to geo location Mapping service.

- 1,242
- 1
- 13
- 24