Skip to content

a light-weight yet powerful memory efficient scalable software solution for companies or organizations that need to restrict access to their services based on geographic location or whatever their needs may be.

Notifications You must be signed in to change notification settings

ahmedabougabal/dotnet-ip-geolocation-api

Repository files navigation

Country Blocking API: Geolocation-Based Access Control

Project Status

Code Documentation (from a business logic Perspective)

idea behind Finally implemented a light-weight yet powerful and a scalable also an effeiciant software solution for companies or organizations that need to restrict access to their services based on geographic location or whatever their needs may be.


key features and what makes it ideal & Great!

  • in-memory data storage without database dependencies
  • thread-safe under concurrent operations. (refer to microsoft docs about concurrent dictionaries/hashtables)
  • third-party geolocation integration
  • comprehensive validation and testing at every level
  • backround processing of temporary expired countries without performance overhead

How to Install and Run the Country Blocking API

Follow these steps to install and run the Country Blocking API on your local machine:

Prerequisites

  • .NET 8.0 SDK or later installed on your machine
  • Git (to clone the repository)

Installation Steps

  1. Clone the repository:

    <git clone SSH>
    git clone https://github.com/ahmedabougabal/dotnet-ip-geolocation-api.git
  2. Navigate to the project directory:

    cd dotnet-ip-geolocation-api
  3. Build the application:

    dotnet build
  4. Run the application:

    dotnet run

this should be what you see

update

Accessing Swagger UI

Once the application is running, you can access the Swagger UI documentation using the following link.

image

This will open the interactive API documentation where you can test all the endpoints directly from your browser.

Note: The port number (5059) might be different on your machine. Check the console output when you run the application to see the actual URL.

Design Patterns & Clean Code Arch & Separation of concerns were carefully taken into consideration when engineering this app.

  • Repository Pattern : to abstract data access logic

  • Dependency Injection : registration of services and repositories using the built-in DI container

  • Service Layer Pattern : The GeolocationService encapsulates external API communication

  • Factory Pattern: HttpClientFactory for creating HttpClient instances

  • Singleton Pattern : repositories are registered as singletons to maintain in-memory state

Testing my Implementation

this was before refactoring the code after recognizing that the input should be the country code only and an ip address is not required based on the assignment


image


this is after, works perfectly without any issues

Desktop Screenshot 2025 03 01 - 17 47 52 69

testing against an invalid country code "XX" as per task that shall return a Bad Request as the country code should be an ISO 3166-1 alpha-2 code.

image

checking for Temporarily expired countires and flush them out from the memory as required

image


An Ip address lookup endpoint that returns all user's data including the timezone, country, location, ISP based on the input IP Address

Desktop Screenshot 2025 03 01 - 17 40 24 29

these are some concerns and key takeaways related to Networking Fundamentals encountered when developing this app and testing it locally

1. Empty Response from /api/logs/blocked-attempts

The /api/logs/blocked-attempts endpoint returns an empty response because no blocked attempts are recorded from outside (a real user with a real IP address from a restricted country) when testing locally, (in short : no actual one from a blocked country tried to access this API/Service)

Thus :

  • No access attempts are made from blocked countries during testing.
  • The logs repository remains empty without sample data.

Solution: I have added a sample data generation for blocked attempts as a mock testing, ensuring the endpoint returns meaningful data for local testing purposes.


2. Local Environment IP Detection Limitations (it is not possible - when running locally - to get the real external IP address from HttpContext so a default public IP is used instead that's all)

The /api/ip/check-block endpoint faces limitations when running locally:

  • On localhost, HttpContext.Connection.RemoteIpAddress returns loopback addresses (::1 or 127.0.0.1), which cannot be geolocated.
  • This prevents accurate external IP detection.

Solution: I added a localEnviroment logic that has a fallback IP (Google DNS: 8.8.8.8) is used for testing, along with the local address, while providing a clear message in the response explaining the limitation.

These solutions I implemented to ensure a smooth addressing testing challenges and offering transparency.


The End :D - made with ❤️ by Ahmed Abou Gabal

About

a light-weight yet powerful memory efficient scalable software solution for companies or organizations that need to restrict access to their services based on geographic location or whatever their needs may be.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages