This project is a .NET 8 Web API that manages country blocking, validates IP addresses using a GeoLocation provider,
and logs blocked access attempts โ all using in-memory storage (no database).
The main goal of this project is to build an API that allows:
- Blocking specific countries permanently or temporarily.
- Automatically unblocking temporary blocks after their duration expires.
- Checking any incoming IP (or caller IP) against the blocked list.
- Logging blocked attempts with details like IP, country, timestamp, and user agent.
- Providing all API documentation through Swagger.
Everything runs in-memory, so no database is required.
Data is stored using a thread-safe ConcurrentDictionary, and a background service handles cleanup for expired temporary blocks.
- .NET 8 Web API
- C# 12
- Swagger
- FluentValidation (with AutoValidation)
- HttpClient for calling GeoLocation API
- Hosted BackgroundService for cleanup jobs
- ILogger / Dependency Injection
- Options Pattern for strongly-typed configuration
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/countries/block |
Block a country permanently |
| POST | /api/countries/temporal-block |
Temporarily block a country for a specific duration |
| DELETE | /api/countries/block/{countryCode} |
Unblock a previously blocked country |
| GET | /api/countries/blocked?page=1&pageSize=10&search=eg |
List all blocked countries with pagination & search |
| GET | /api/ip/lookup?ipAddress=8.8.8.8 |
Get GeoLocation info for any IP |
| GET | /api/ip/check-block |
Check if the callerโs IP is blocked |
| GET | /api/logs/blocked-attempts?page=1&pageSize=10 |
Retrieve paginated logs of blocked attempts |
๐ฆ A-Technologies-Assignment
โฃ ๐ Controllers
โ โฃ CountriesController.cs
โ โฃ IpController.cs
โ โ LogsController.cs
โฃ ๐ Services
โ โฃ BlockService.cs
โ โ GeoLocationService.cs
โฃ ๐ Repositories
โ โ InMemoryBlockedRepository.cs
โฃ ๐ BackgroundJobs
โ โ TemporalBlockCleanupService.cs
โฃ ๐ DTOs / Models / Validators / Settings
โ Program.cs
- Controllers ๏ฟฝ๏ฟฝ Handle API endpoints and HTTP responses.
- Services โ Contain business logic for blocking, checking, and Geo lookups.
- Repositories โ Manage in-memory data storage.
- Validators โ Apply FluentValidation rules for request validation.
- BackgroundJobs โ Run scheduled cleanup of expired temporary blocks.
- Settings โ Hold strongly-typed configuration for GeoLocation API.
- Permanent & Temporary blocking with expiration.
- Automatic cleanup for expired blocks every 5 minutes.
- GeoLocation integration using
ipapi.com(async via HttpClient). - Thread-safe in-memory storage using
ConcurrentDictionary. - Pagination and search support for blocked countries and logs.
- Logging system to record all blocked access attempts.
- Options Pattern + Validation for startup configuration safety.
- Swagger Documentation with XML comments and clear grouping by controllers.
All API settings are stored in appsettings.json:
"GeoLocationSettings": {
"BaseUrl": "https://api.ipapi.com/api",
"ApiKey": "<API_KEY>"
}If the API key is missing or invalid, the app will not start โ thanks to:
builder.Services.AddOptions<GeoLocationSettings>()
.BindConfiguration(GeoLocationSettings.SectionName)
.ValidateDataAnnotations()
.ValidateOnStart();{
"IpAddress": "102.45.33.19",
"CountryCode": "EG",
"IsBlocked": true,
"UserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
"Timestamp": "2025-10-27T20:10:45Z"
}- Developed by: Mahmoud Mohamed
- Email: mahmoud.abdalaziz@outlook.com
- GitHub: M7-TROJAN
- LinkedIn: Mahmoud Mohamed Abd