This guide walks you through detecting VPN, proxy, TOR, and bot IP addresses in C# using HttpClient and the Focsec API.

The examples use modern async/await patterns and work with .NET 6+ and ASP.NET Core applications.

1

Create the response model

Define a class to deserialize the API response:

csharp
using System.Text.Json.Serialization;

public class IpResponse
{
    [JsonPropertyName("ip")]
    public string Ip { get; set; }

    [JsonPropertyName("is_vpn")]
    public bool IsVpn { get; set; }

    [JsonPropertyName("is_proxy")]
    public bool IsProxy { get; set; }

    [JsonPropertyName("is_tor")]
    public bool IsTor { get; set; }

    [JsonPropertyName("is_bot")]
    public bool IsBot { get; set; }

    [JsonPropertyName("is_datacenter")]
    public bool IsDatacenter { get; set; }

    [JsonPropertyName("city")]
    public string City { get; set; }

    [JsonPropertyName("country")]
    public string Country { get; set; }

    [JsonPropertyName("iso_code")]
    public string IsoCode { get; set; }

    [JsonPropertyName("is_in_european_union")]
    public bool IsInEuropeanUnion { get; set; }

    [JsonPropertyName("flag")]
    public string Flag { get; set; }

    [JsonPropertyName("autonomous_system_number")]
    public int AutonomousSystemNumber { get; set; }

    [JsonPropertyName("autonomous_system_organization")]
    public string AutonomousSystemOrganization { get; set; }

    public bool IsSuspicious =>
        IsVpn || IsProxy || IsTor || IsBot || IsDatacenter;
}
2

Check an IP for VPN or proxy

Use HttpClient to call the Focsec API.

Get your free API key to start detecting VPNs.

csharp
using System.Net.Http.Headers;
using System.Text.Json;

public class FocsecClient
{
    private readonly HttpClient _httpClient;
    private readonly string _apiKey;

    public FocsecClient(string apiKey)
    {
        _apiKey = apiKey;
        _httpClient = new HttpClient
        {
            BaseAddress = new Uri("https://api.focsec.com")
        };
    }

    public async Task<IpResponse> CheckIpAsync(string ipAddress)
    {
        var request = new HttpRequestMessage(
            HttpMethod.Get,
            $"/v1/ip/{ipAddress}"
        );
        request.Headers.Authorization =
            new AuthenticationHeaderValue(_apiKey);

        var response = await _httpClient.SendAsync(request);
        response.EnsureSuccessStatusCode();

        var json = await response.Content.ReadAsStringAsync();
        return JsonSerializer.Deserialize<IpResponse>(json);
    }
}

// Usage
var client = new FocsecClient("your-api-key-here");
var result = await client.CheckIpAsync("82.40.11.200");

Console.WriteLine($"IP: {result.Ip}");
Console.WriteLine($"VPN: {result.IsVpn}, Proxy: {result.IsProxy}");

API Response

The API returns detection flags and location data as JSON:

Response
{
  "ip": "82.40.11.200",
  "is_vpn": false,
  "is_proxy": false,
  "is_bot": false,
  "is_tor": false,
  "is_datacenter": false,
  "city": "London",
  "country": "United Kingdom",
  "iso_code": "gb",
  "is_in_european_union": false,
  "flag": "šŸ‡¬šŸ‡§",
  "autonomous_system_number": 5089,
  "autonomous_system_organization": "Virgin Media Limited"
}
3

ASP.NET Core middleware

Create middleware to block VPN users from sensitive endpoints:

csharp
public class VpnDetectionMiddleware
{
    private readonly RequestDelegate _next;
    private readonly FocsecClient _focsec;

    public VpnDetectionMiddleware(
        RequestDelegate next,
        IConfiguration config)
    {
        _next = next;
        _focsec = new FocsecClient(config["Focsec:ApiKey"]);
    }

    public async Task InvokeAsync(HttpContext context)
    {
        // Only check sensitive paths
        if (context.Request.Path.StartsWithSegments("/checkout"))
        {
            var clientIp = context.Connection.RemoteIpAddress?.ToString();

            if (!string.IsNullOrEmpty(clientIp))
            {
                var result = await _focsec.CheckIpAsync(clientIp);

                if (result.IsSuspicious)
                {
                    context.Response.StatusCode = 403;
                    await context.Response.WriteAsJsonAsync(new
                    {
                        error = "VPN or proxy detected. Please disable to continue."
                    });
                    return;
                }
            }
        }

        await _next(context);
    }
}

// In Program.cs or Startup.cs
app.UseMiddleware<VpnDetectionMiddleware>();

Next steps

You've integrated VPN detection into your C# application. See the API Reference for all available fields and options.

Ready to get started?

Sign up for a free account and start detecting VPNs, proxies, and bots in your application.

Get your API key »