DEV Community

Cover image for Using C# to check for proxy and VPN with IP2Location.io geolocation API
IP2Location
IP2Location

Posted on • Updated on

Using C# to check for proxy and VPN with IP2Location.io geolocation API

Intro

The IP2Location.io API is a great way to query an IP address to find its geolocation and proxy data. For C# developers, there is the IP2Location.io .NET SDK which is a wrapper for the API. By using this SDK, there is no need to manually parse and deserialize the JSON result into a usable form. In this article, we’ll explore why you need geolocation and proxy detection. We’ll also show a sample code for using the IP2Location.io .NET SDK to query the IP and get the resulting data.

IP geolocation usage scenarios

If you’re not familiar with IP geolocation, it’s basically the process of mapping an IP address to physical locations. Developers can use geolocation databases or API to check what is the country, region, city, ISP, time zone and so much more just based on the IP address. Common usage scenarios involving geolocation would be showing localized contents on websites, targeted advertising, multilingual websites, auto-filling forms with basic location details, etc. Modern websites heavily rely on IP geolocation to perform most of their functionalities.

Detect proxy usage like VPN and residential proxy

The benefit of using the IP2Location.io API is that the data also includes whether the IP belongs to a proxy server. Being able to tell if the user browsing a website is using a proxy server is a necessity, especially for e-commerce websites. Fraudsters commonly hide their IP geolocation using proxy servers like VPN or residential proxy servers when making online purchases with stolen cards. This makes it harder for online stores to check whether the order is a fraudulent one. Fraud detection normally checks IP geolocation countries vs. the shipping or billing address. By using a proxy that is located in the same country as the address, fraudsters can potentially slip through the fraud checking mechanism. Hence, it’s vital for websites to have proxy server detection.

Prerequisites for running the code

There are some requirements before you can run the example code that we’ll show below.

  1. Sign up for an IP2Location.io API key at https://www.ip2location.io/pricing if you don’t have one. The free plan comes with basic geolocation data which can be useful to get started.
  2. You’ll need Visual Studio 2022 or later to compile and run our code.

Let’s create a new C# project for our demo

Open up your Visual Studio and click on Create a new project.
Then select Console App as in the screenshot below and click Next.

IP2Location.io geolocation API

Next, we’ll call our project DemoIP2LocationIO as below and click Next.

IP2Location.io geolocation API

After that, we’ll select .NET 7.0 as our framework and click Create.

IP2Location.io geolocation API

Now, we should see the editor like below.

IP2Location.io geolocation API

Before we edit the code, install the NuGet package which contains the IP2Location.io .NET SDK that will allow us to use the IP2Location.io API. We’ll also need to install the JSON parser.

In the Visual Studio menu, click on Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution, then click on the Newtonsoft.Json package to install it.

IP2Location.io geolocation API

Do the same for the IP2Location.io package. You may need to search “IP2Location.io” to find it.

IP2Location.io geolocation API

Now, let’s modify the existing code with our code

Let’s replace the code in Program.cs with the below code. Remember to edit the code to use your IP2Location.io API key that you’ve signed up for above.

using Newtonsoft.Json;
using IP2LocationIOComponent;

// Configures IP2Location.io API key
Configuration Config = new()
{
    ApiKey = "YOUR_API_KEY"
};

IPGeolocation IPL = new(Config);
string IP = "8.8.8.8";
string Lang = "en"; // the language param is only available with Plus and Security plans

// Lookup ip address geolocation data
var MyTask = IPL.Lookup(IP, Lang); // async API call

try
{
    var MyObj = MyTask.Result;

    // pretty-print JSON
    Console.WriteLine(JsonConvert.SerializeObject(MyObj, Formatting.Indented));

    // individual fields
    Console.WriteLine("ip: {0}", MyObj["ip"]);
    Console.WriteLine("country_code: {0}", MyObj["country_code"]);
    Console.WriteLine("country_name: {0}", MyObj["country_name"]);
    Console.WriteLine("region_name: {0}", MyObj["region_name"]);
    Console.WriteLine("city_name: {0}", MyObj["city_name"]);
    Console.WriteLine("latitude: {0}", MyObj["latitude"]);
    Console.WriteLine("longitude: {0}", MyObj["longitude"]);
    Console.WriteLine("zip_code: {0}", MyObj["zip_code"]);
    Console.WriteLine("time_zone: {0}", MyObj["time_zone"]);
    Console.WriteLine("asn: {0}", MyObj["asn"]);
    Console.WriteLine("as: {0}", MyObj["as"]);
    if (MyObj["isp"] != null) Console.WriteLine("isp: {0}", MyObj["isp"]);
    if (MyObj["domain"] != null) Console.WriteLine("domain: {0}", MyObj["domain"]);
    if (MyObj["net_speed"] != null) Console.WriteLine("net_speed: {0}", MyObj["net_speed"]);
    if (MyObj["idd_code"] != null) Console.WriteLine("idd_code: {0}", MyObj["idd_code"]);
    if (MyObj["area_code"] != null) Console.WriteLine("area_code: {0}", MyObj["area_code"]);
    if (MyObj["weather_station_code"] != null) Console.WriteLine("weather_station_code: {0}", MyObj["weather_station_code"]);
    if (MyObj["weather_station_name"] != null) Console.WriteLine("weather_station_name: {0}", MyObj["weather_station_name"]);
    if (MyObj["mcc"] != null) Console.WriteLine("mcc: {0}", MyObj["mcc"]);
    if (MyObj["mnc"] != null) Console.WriteLine("mnc: {0}", MyObj["mnc"]);
    if (MyObj["mobile_brand"] != null) Console.WriteLine("mobile_brand: {0}", MyObj["mobile_brand"]);
    if (MyObj["elevation"] != null) Console.WriteLine("elevation: {0}", MyObj["elevation"]);
    if (MyObj["usage_type"] != null) Console.WriteLine("usage_type: {0}", MyObj["usage_type"]);
    if (MyObj["address_type"] != null) Console.WriteLine("address_type: {0}", MyObj["address_type"]);
    if (MyObj["district"] != null) Console.WriteLine("district: {0}", MyObj["district"]);
    if (MyObj["ads_category"] != null) Console.WriteLine("ads_category: {0}", MyObj["ads_category"]);
    if (MyObj["ads_category_name"] != null) Console.WriteLine("ads_category_name: {0}", MyObj["ads_category_name"]);
    Console.WriteLine("is_proxy: {0}", MyObj["is_proxy"]);

    if (MyObj["continent"] != null)
    {
        var Continent = MyObj["continent"];
        Console.WriteLine("continent => name: {0}", Continent["name"]);
        Console.WriteLine("continent => code: {0}", Continent["code"]);
        Console.WriteLine("continent => hemisphere: {0}", Continent["hemisphere"]);
        Console.WriteLine("continent => translation => lang: {0}", Continent["translation"]["lang"]);
        Console.WriteLine("continent => translation => value: {0}", Continent["translation"]["value"]);
    }

    if (MyObj["country"] != null)
    {
        var Country = MyObj["country"];
        Console.WriteLine("country => name: {0}", Country["name"]);
        Console.WriteLine("country => alpha3_code: {0}", Country["alpha3_code"]);
        Console.WriteLine("country => numeric_code: {0}", Country["numeric_code"]);
        Console.WriteLine("country => demonym: {0}", Country["demonym"]);
        Console.WriteLine("country => flag: {0}", Country["flag"]);
        Console.WriteLine("country => capital: {0}", Country["capital"]);
        Console.WriteLine("country => total_area: {0}", Country["total_area"]);
        Console.WriteLine("country => population: {0}", Country["population"]);
        Console.WriteLine("country => tld: {0}", Country["tld"]);

        Console.WriteLine("country => currency => code: {0}", Country["currency"]["code"]);
        Console.WriteLine("country => currency => name: {0}", Country["currency"]["name"]);
        Console.WriteLine("country => currency => symbol: {0}", Country["currency"]["symbol"]);

        Console.WriteLine("country => language => code: {0}", Country["language"]["code"]);
        Console.WriteLine("country => language => name: {0}", Country["language"]["name"]);

        Console.WriteLine("country => translation => lang: {0}", Country["translation"]["lang"]);
        Console.WriteLine("country => translation => value: {0}", Country["translation"]["value"]);
    }

    if (MyObj["region"] != null)
    {
        var Region = MyObj["region"];
        Console.WriteLine("region => name: {0}", Region["name"]);
        Console.WriteLine("region => code: {0}", Region["code"]);

        Console.WriteLine("region => translation => lang: {0}", Region["translation"]["lang"]);
        Console.WriteLine("region => translation => value: {0}", Region["translation"]["value"]);
    }

    if (MyObj["city"] != null)
    {
        var City = MyObj["city"];
        Console.WriteLine("city => name: {0}", City["name"]);

        Console.WriteLine("city => translation => lang: {0}", City["translation"]["lang"]);
        Console.WriteLine("city => translation => value: {0}", City["translation"]["value"]);
    }

    if (MyObj["time_zone_info"] != null)
    {
        var TimeZone = MyObj["time_zone_info"];
        Console.WriteLine("time_zone_info => olson: {0}", TimeZone["time_zone_info"]);
        Console.WriteLine("time_zone_info => current_time: {0}", TimeZone["current_time"]);
        Console.WriteLine("time_zone_info => gmt_offset: {0}", TimeZone["gmt_offset"]);
        Console.WriteLine("time_zone_info => is_dst: {0}", TimeZone["is_dst"]);
        Console.WriteLine("time_zone_info => sunrise: {0}", TimeZone["sunrise"]);
        Console.WriteLine("time_zone_info => sunset: {0}", TimeZone["sunset"]);
    }

    if (MyObj["geotargeting"] != null)
    {
        var GeoTarget = MyObj["geotargeting"];
        Console.WriteLine("geotargeting => metro: {0}", GeoTarget["metro"]);
    }

    if (MyObj["proxy"] != null)
    {
        var Proxy = MyObj["proxy"];
        Console.WriteLine("proxy => last_seen: {0}", Proxy["last_seen"]);
        Console.WriteLine("proxy => proxy_type: {0}", Proxy["proxy_type"]);
        Console.WriteLine("proxy => threat: {0}", Proxy["threat"]);
        Console.WriteLine("proxy => provider: {0}", Proxy["provider"]);
        Console.WriteLine("proxy => is_vpn: {0}", Proxy["is_vpn"]);
        Console.WriteLine("proxy => is_tor: {0}", Proxy["is_tor"]);
        Console.WriteLine("proxy => is_data_center: {0}", Proxy["is_data_center"]);
        Console.WriteLine("proxy => is_public_proxy: {0}", Proxy["is_public_proxy"]);
        Console.WriteLine("proxy => is_web_proxy: {0}", Proxy["is_web_proxy"]);
        Console.WriteLine("proxy => is_web_crawler: {0}", Proxy["is_web_crawler"]);
        Console.WriteLine("proxy => is_residential_proxy: {0}", Proxy["is_residential_proxy"]);
        Console.WriteLine("proxy => is_spammer: {0}", Proxy["is_spammer"]);
        Console.WriteLine("proxy => is_scanner: {0}", Proxy["is_scanner"]);
        Console.WriteLine("proxy => is_botnet: {0}", Proxy["is_botnet"]);
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
Enter fullscreen mode Exit fullscreen mode

Run the code to query geolocation and proxy data for the 8.8.8.8 IP address. You should see the below output. There are 2 sections in the output. In our code, we will output the raw JSON result in pretty-print format. This is so you can see what is actually returned by the API. Then we will output the individual fields if they exist. For the individual fields, any that is shown with “=>” means it’s a nested field.

NOTE: Our example is using the Security plan so we have access to all the fields. If you’re not subscribed to the Security plan, you will see less data.

{
  "ip": "8.8.8.8",
  "country_code": "US",
  "country_name": "United States of America",
  "region_name": "California",
  "city_name": "Mountain View",
  "latitude": 37.38605,
  "longitude": -122.08385,
  "zip_code": "94035",
  "time_zone": "-08:00",
  "asn": "15169",
  "as": "Google LLC",
  "isp": "Google LLC",
  "domain": "google.com",
  "net_speed": "T1",
  "idd_code": "1",
  "area_code": "650",
  "weather_station_code": "USCA0746",
  "weather_station_name": "Mountain View",
  "mcc": "-",
  "mnc": "-",
  "mobile_brand": "-",
  "elevation": 32,
  "usage_type": "DCH",
  "address_type": "Anycast",
  "continent": {
    "name": "North America",
    "code": "NA",
    "hemisphere": [
      "north",
      "west"
    ],
    "translation": {
      "lang": "en",
      "value": "North America"
    }
  },
  "district": "Santa Clara County",
  "country": {
    "name": "United States of America",
    "alpha3_code": "USA",
    "numeric_code": 840,
    "demonym": "Americans",
    "flag": "https://cdn.ip2location.io/assets/img/flags/us.png",
    "capital": "Washington, D.C.",
    "total_area": 9826675,
    "population": 331002651,
    "currency": {
      "code": "USD",
      "name": "United States Dollar",
      "symbol": "$"
    },
    "language": {
      "code": "EN",
      "name": "English"
    },
    "tld": "us",
    "translation": {
      "lang": "en",
      "value": "United States of America"
    }
  },
  "region": {
    "name": "California",
    "code": "US-CA",
    "translation": {
      "lang": "en",
      "value": "California"
    }
  },
  "city": {
    "name": "Mountain View",
    "translation": {
      "lang": "en",
      "value": "Mountain View"
    }
  },
  "time_zone_info": {
    "olson": "America/Los_Angeles",
    "current_time": "2023-11-23T09:32:39+08:00",
    "gmt_offset": -28800,
    "is_dst": false,
    "sunrise": "06:56",
    "sunset": "16:53"
  },
  "geotargeting": {
    "metro": "807"
  },
  "ads_category": "IAB19-11",
  "ads_category_name": "Data Centers",
  "is_proxy": false,
  "proxy": {
    "last_seen": 22,
    "proxy_type": "DCH",
    "threat": "-",
    "provider": "-",
    "is_vpn": false,
    "is_tor": false,
    "is_data_center": true,
    "is_public_proxy": false,
    "is_web_proxy": false,
    "is_web_crawler": false,
    "is_residential_proxy": false,
    "is_spammer": false,
    "is_scanner": false,
    "is_botnet": false
  }
}
ip: 8.8.8.8
country_code: US
country_name: United States of America
region_name: California
city_name: Mountain View
latitude: 37.38605
longitude: -122.08385
zip_code: 94035
time_zone: -08:00
asn: 15169
as: Google LLC
isp: Google LLC
domain: google.com
net_speed: T1
idd_code: 1
area_code: 650
weather_station_code: USCA0746
weather_station_name: Mountain View
mcc: -
mnc: -
mobile_brand: -
elevation: 32
usage_type: DCH
address_type: Anycast
district: Santa Clara County
ads_category: IAB19-11
ads_category_name: Data Centers
is_proxy: False
continent => name: North America
continent => code: NA
continent => hemisphere: [
  "north",
  "west"
]
continent => translation => lang: en
continent => translation => value: North America
country => name: United States of America
country => alpha3_code: USA
country => numeric_code: 840
country => demonym: Americans
country => flag: https://cdn.ip2location.io/assets/img/flags/us.png
country => capital: Washington, D.C.
country => total_area: 9826675
country => population: 331002651
country => tld: us
country => currency => code: USD
country => currency => name: United States Dollar
country => currency => symbol: $
country => language => code: EN
country => language => name: English
country => translation => lang: en
country => translation => value: United States of America
region => name: California
region => code: US-CA
region => translation => lang: en
region => translation => value: California
city => name: Mountain View
city => translation => lang: en
city => translation => value: Mountain View
time_zone_info => olson:
time_zone_info => current_time: 23/11/2023 9:32:39 AM
time_zone_info => gmt_offset: -28800
time_zone_info => is_dst: False
time_zone_info => sunrise: 06:56
time_zone_info => sunset: 16:53
geotargeting => metro: 807
proxy => last_seen: 22
proxy => proxy_type: DCH
proxy => threat: -
proxy => provider: -
proxy => is_vpn: False
proxy => is_tor: False
proxy => is_data_center: True
proxy => is_public_proxy: False
proxy => is_web_proxy: False
proxy => is_web_crawler: False
proxy => is_residential_proxy: False
proxy => is_spammer: False
proxy => is_scanner: False
proxy => is_botnet: False
Enter fullscreen mode Exit fullscreen mode

With such an extensive amount of data returned by the IP2Location.io API, C# developers can easily determine what country, region and city the website visitor is from. In addition, IP addresses from logs can be checked against the geolocation and proxy data to make sure that there is no unauthorized access to networks or servers. Marketing analytics can also be enriched with such detailed geolocation to enable better use of the marketing resources.

As for online merchants, it is very important to check whether the is_vpn or is_residential_proxy nested field is true. If either is true, merchants need to decide if they wish to block such IP addresses from accessing the services or making purchases.

Conclusion

It is that easy to integrate the IP2Location.io API and its comprehensive data into any C# projects. The benefits reaped from such data can help to boost revenue, block frauds and strengthen organizational security posture.

Top comments (0)