views:

678

answers:

7

How would i determine the country that a spcific IP address is originating from using c#. I need to use this to check if connections originate from a specific country.

+12  A: 

You can use this SQL data in your project to determine that: IP address geolocation SQL database. Download that data and import it into your database to run checks locally.

Or you can use their free API that returns XML containing the country code and country name. You'd make a request to the following URL with the IP address you wanted to check, as seen in this example:

http://ipinfodb.com/ip_query_country.php?ip=74.125.45.100

Returns:

<Response>
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
</Response>
Ronnie
Please note that the IP assignments change occasionally. So remember to update your database occasionally. (The frequency of updates you need depends on what you're doing; for simple stats you may decide that annual updates are OK).
user9876
+1  A: 

If you don't want to use an API like perhaps hostip.info, then I'd suggest subscribing to maxmind and running a host lookup database locally.

david bowies labyrinth crotch
+2  A: 

you can ask google to do it for you.

there are also services that you can pay for you want:

YetAnotherDeveloper
+2  A: 

Here is a free IP Address to Country database.

JP Alioto
A: 

There are Geo location databases that do a GEO lookup. That might help.

+1  A: 

ip2cc - Lookup country and Russia region by IP address Python module with script to create database from up-to-date official data.

This Python utility loads (as frequently as you like) up-to-date information from Regional Internet Registry sites (arin, ripencc, apnic, lacnic, afrinic), as shown in the source:

url_template = 'ftp://ftp.ripe.net/pub/stats/%s/delegated-%s-latest'
sources = {}
for name in ('arin', 'ripencc', 'apnic', 'lacnic', 'afrinic'):
    sources[name] = url_template % (name, name)

Once the data is loaded, queries can be answered offline and very quickly. Can be easily modified to directly answer the original question, or used from the command line to return the country an IP address belongs to.

gimel
A: 

If only you could do it online because there are free APIs and web services out there you can query for the latest data. Also you won't have to worry about outdated data as generally speaking these services do tend to keep their data up-to-date.

Since you wanted an offline option, your best option would be a paid service which allow you to download the latest data every month like IP2Location.

James