Reputation:
I want to implement this in ASP.NET. I don't have any idea about how to do so, unfortunately.
Upvotes: 18
Views: 46863
Reputation: 18549
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
}
}
Upvotes: 1
Reputation: 4521
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
Upvotes: 4
Reputation:
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.
Upvotes: 2
Reputation: 3430
Here how it is done in asp.net
Request.ServerVariables("REMOTE_ADDR")
Get a copy of IP adress database by location here
Upvotes: 10
Reputation: 86957
IPAddressExtensions is a free codeplex class library if all you just want is the Country the IP is located from.
Upvotes: 2
Reputation: 11397
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 .
Upvotes: 4
Reputation: 23016
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.
Upvotes: 5
Reputation: 1242
Get the client IP and find the location of IP using any IP to geo location Mapping service.
Upvotes: 0