• nbolt
  • [ 5.0 , 1 ]
login to rate this
.NET BlogEngine 4.0 [non-official]
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
[.NET7] Design Patterns: Facade in a Web Application
5 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
[.NET7] Design Patterns: Repository in a Web Application
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
[.NET7] IP GeoLocation with Dependency Injection and Custom Middleware
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
[.NET7] Weather Forecast with Dependency Injection and Custom Middleware
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
2 ways for creating QR Code using ASP.NET
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
AMD Ryzen 9 home server with Fedora 41 and .NET 8
15 1
  • ☺️ 1
    ❤️ 2
    🤩 1
    🫠 1
    😁 3
    😅 2
    😆 2
    😉 1
    😊 1
    😗 1
    😘 1
    😚 1
    🙂 1
    🙃 1
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
ASP.NET MVC Routing with @
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
ASP.NET MVC Routing with Constraints
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
BlogEngine.NET 3.3.5 with a BBQ Sauce
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
BlogEngine.NET 3.3.5 with a BBQ Sauce: Part 2
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
EF7 on .NET Core 7 (Preview): Fedora 35 x Windows 11 (41 Million Rows)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Entity Framework Core 6: Fedora 35 x Windows 11 (41 Million Rows)
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
Entity Framework Core 7 (Preview): Fedora 35 x Windows 11 (41 Million Rows)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Entity Framework Providers for the .NET BlogEngine
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
Enum as String in EF Core
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Facebook: Data Deletion Callback URL in C#
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
How to fix the Could not load file or assembly 'System.IO.Compression' or one of its dependencies exception
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
How to Recover Access to your Account in an ASP.NET Application using the Membership (without decrypting your password)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Installing and Configuring Jenkins with GIT to Build and Deploy Your .NET Application
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Installing and Configuring Jenkins with SVN to Build and Deploy Your .NET Application
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
IP GeoLocation with Unity and Dependency Injection
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
NHibernate Providers for the .NET BlogEngine
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
No mercy for the ORMs: Load and Performance Test on EF Core, NHibernate and pure ADO.NET (41M rows)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Resizing images on a Controller Method in ASP.NET Core (.NET 6)
0 0
  • 🔥 1
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Streaming Video Files in ASP.NET MVC
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Testing your MVC App on XSP/mono in Fedora (before deploying to Apache)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Weather with Unity and Dependency Injection
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
[.NET7] IP GeoLocation with Dependency Injection and Custom Middleware
0 0

Hi Folks! In this post I will show you how to retrieve the user's geolocation from their IP address in a .NET7 application. This application relies on multiple geolocation providers (IPInfoDb being one of them and requiring registration in order to function properly) and a custom middleware, to execute the service(s) for each and every request.

First, let's start off with cloning the github repository:

Before running the application, you need to register yourself on IpInfoDb to get an API Key.

You can clone the repository here.

Open the NET7-IPGeolocation.sln solution file and go straight to the ILocationService interface (the heart of the whole thing), inside Services.IPLocation project. You will notice that the interface defines the contract for a service of the ip-geolocation. The most important one being the Find method (that actually calls the provider themselves to retrieve the location).

Basically, that interface contains objects for thresholds, API limits, number of successful and unsuccessful calls; and where the service should save their information in cache (keys). Most of the implementation can be seen inside the abstract LocationService class. But there's 5 concrete classes implementing the ip-geolocation service:

  1. DummyIp - do not use;
  2. FreeGeoIp - decomissioned, do not use;
  3. GeoPlugin;
  4. IpApi; and
  5. IpInfoDb.

You can always create a new concrete LocationService class for a provider of your choosing.

So now, let's take a look at the custom middleware, called LocationMiddleware, orchestrating the calls and which instance of the service to use (there's an extension method called Next inside the Extensions.cs file of the Services.IPLocation project. Head to the Location webapplication project and a folder called Middleware, there will only be one file there.

As you can see, there dependency injection in the constructor of the middleware, one for the request pipeline (default for a middleware) and another one for the implementers of the ILocationService. At the execution of the request, the middleware tries to retrieve the real ip address of the client (because the local ip address will always be ::1) and then retrieve the appropriate ILocationService instance from the collection.

After calling the Find method and obtaining the location, it saves the information in session (before heading to the next request middleware).

Now, go to the Program.cs file inside the Location web application. At the end of it, you will see a MapGet call for the "/" path, basically getting the information back from Session and returning the user's location in a string form. 

Before running the application, you need to register yourself on IpInfoDb to get an API Key.

Aftet that, you need to replace "INSERT YOUR KEY HERE" string inside the Program.cs file in the Location project.

Run the application, you should see a page in your browser stating your current location (from your IP address alone, no location sharing necessary).

Hope you guys enjoyed it. See ya!

what to see next
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
IP GeoLocation with Unity and Dependency Injection
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
[.NET7] Weather Forecast with Dependency Injection and Custom Middleware
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
[.NET7] Design Patterns: Facade in a Web Application
5 1
comment section
place your comment here, but login first
be the first to comment
An error has occurred . This application may no longer respond until reloaded . An unhandled exception has occurred . See browser dev tools for details . Reload 🗙