There’s a few APIs available which allow you to provide an IP address and to retrieve details back about it including it’s aproximate location.

I was previously using ipstack for this but their free tier is pretty poor as it only provides 100 calls a month so I moved over to using ipregistry which provides 100,000 free calls a month and more detailed information about the IP address.

In order to make accessing the API from C# easier I created a client library which has been published to NuGet.

I mostly use services via Dependency Injection so I’ve added an extension method to help with the setup.

// Add API client
serviceCollection.AddIpRegistry(options =>
    {
        options.ApiKey = "API KEY";
    });

The injected client can then be used like so.

var ipMyAddressDetails = await _ipRegistryService.GetIpAddressDetailsAsync();

var ipTargetAddressDetails = await _ipRegistryService.GetIpAddressDetailsAsync("66.165.2.7");

var ipTargetBatchAddressDetails = await _ipRegistryService.GetIpAddressDetailsAsync(new List<string>()
{
    "66.165.2.7", "2a01:e35:2f22:e3d0::2", "1.1.1.1"
});

var myUserAgentDetails = await _ipRegistryService.GetUserAgentDetailsAsync();

var targetUserAgentDetails = await _ipRegistryService.GetUserAgentDetailsAsync("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0");

var targetUserAgentBatchDetails = await _ipRegistryService.GetUserAgentDetailsAsync(new List<string>()
{
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36"
});

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.