GeoIpServices 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package GeoIpServices --version 3.0.0                
NuGet\Install-Package GeoIpServices -Version 3.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GeoIpServices" Version="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GeoIpServices --version 3.0.0                
#r "nuget: GeoIpServices, 3.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GeoIpServices as a Cake Addin
#addin nuget:?package=GeoIpServices&version=3.0.0

// Install GeoIpServices as a Cake Tool
#tool nuget:?package=GeoIpServices&version=3.0.0                

GeoIpServices(https://www.nuget.org/packages/GeoIpServices)

GeoIpServices is an open-source C# class library that provides a wrapper around existing services that serve information about the IP. The service stores information in a MongoDb database that you configure using the package MongoDbService

Features

  • Covers IpStack
  • Cache information in your own MongoDB instance to reduce usage of the various services

Contributing

We welcome contributions! If you find a bug, have an idea for improvement, please submit an issue or a pull request on GitHub.

Getting Started

NuGet Package

To include GeoIpServices in your project, install the NuGet package:

dotnet add package GeoIpServices

Then in your appsettings.json add the following sample configuration and change the values to mtch the details of your MongoDB instance.

  "GeoIpSettings": {
    "Controls": {
      "SessionTimeoutInSeconds": 240,
      "MaxRoundRobinAttempts": 2,
      "Priority": [ "IpStack" ]
    },
    "IpStack": {
      "ApiPrefix": "https://api.ipstack.com/",
      "ApiPostfix": "MovedToSecret"
    }
  }

After the above is done, you can just Dependency inject the GeoIpService in your C# class.

For example:

Then you have an endpoint that gives you info about the IP of the visitor.

using GeoIpServices;
using Microsoft.AspNetCore.Mvc;
using MongoDbService;
using System.Net;
using System.Net.Sockets;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddMongoDbServices();
builder.Services.AddGeoIpServices();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
	app.UseSwagger();
	app.UseSwaggerUI();
}


app.MapGet("/ipinfo", async ([FromServices] GeoIpService geoIpService, HttpRequest httpRequest) =>
{
	return await geoIpService.GetGeoIpInfoFromIpv4(GetOriginIpV4(httpRequest));
})
.WithName("GetGeoIpInfoFromIpv4")
.WithOpenApi();

IPAddress? GetOriginIpV4(HttpRequest httpRequest)
{
	var xForwardedForHeader = httpRequest.Headers["X-Forwarded-For"];
	var ipString = xForwardedForHeader.Select(s => s.Trim()).FirstOrDefault();
	if (string.IsNullOrWhiteSpace(ipString) || !IPAddress.TryParse(ipString, out IPAddress? clientIpAddress))
	{
		return null;
	}
	if (clientIpAddress.AddressFamily == AddressFamily.InterNetworkV6)
	{
		// If it's an IPv6 address, convert to IPv4
		return clientIpAddress.MapToIPv4();
	}
	return clientIpAddress;
}

app.Run();

GitHub Repository

Visit our GitHub repository for the latest updates, documentation, and community contributions. https://github.com/prmeyn/GeoIpServices

License

This project is licensed under the GNU GENERAL PUBLIC LICENSE.

Happy coding! 🚀🌐📚

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.0 170 8/11/2024
3.1.0 178 7/8/2024
3.0.0 108 7/8/2024
2.0.5 123 7/8/2024
2.0.4 115 7/7/2024
2.0.3 114 7/7/2024
2.0.0 115 7/7/2024
1.0.0 110 7/7/2024