MandalaConsulting.APIMiddlewares 0.0.14

dotnet add package MandalaConsulting.APIMiddlewares --version 0.0.14
                    
NuGet\Install-Package MandalaConsulting.APIMiddlewares -Version 0.0.14
                    
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="MandalaConsulting.APIMiddlewares" Version="0.0.14" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MandalaConsulting.APIMiddlewares" Version="0.0.14" />
                    
Directory.Packages.props
<PackageReference Include="MandalaConsulting.APIMiddlewares" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MandalaConsulting.APIMiddlewares --version 0.0.14
                    
#r "nuget: MandalaConsulting.APIMiddlewares, 0.0.14"
                    
#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.
#:package MandalaConsulting.APIMiddlewares@0.0.14
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MandalaConsulting.APIMiddlewares&version=0.0.14
                    
Install as a Cake Addin
#tool nuget:?package=MandalaConsulting.APIMiddlewares&version=0.0.14
                    
Install as a Cake Tool

MandalaConsulting.APIMiddleware

Overview

The MandalaConsulting.APIMiddleware library is a comprehensive middleware solution for ASP.NET Core applications. It provides tools for security, performance optimization, and API monitoring. Features include IP blacklisting, invalid endpoint tracking, memory management, and API key validation, designed to enhance the reliability and security of your APIs.


Features

Security and Monitoring

  • IP Blacklisting: Automatically blocks malicious IPs after repeated unauthorized attempts.
  • Invalid Endpoint Tracking: Monitors failed attempts to access non-existent endpoints and prevents abuse.
  • API Key Validation: Ensures requests are authorized with the correct API key.
  • Logging: Captures detailed logs of unauthorized access attempts and API interactions.

Performance Optimization

  • Memory Management: Includes garbage collection based on endpoint usage patterns.
  • Idle Endpoint Detection: Identifies idle endpoints to optimize resource usage.

Flexible Middleware

  • Designed to be easily integrated into any ASP.NET Core application.

Installation

Add the library to your ASP.NET Core project and reference the necessary namespaces:

using MandalaConsulting.APIMiddleware;
using MandalaConsulting.APIMiddleware.Filters;
using MandalaConsulting.APIMiddleware.Utility;
using MandalaConsulting.Logging;

Usage

Middleware Components

InvalidEndpointTrackerMiddleware

Tracks attempts to access invalid endpoints and blocks malicious activity.

Example Integration
public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware<InvalidEndpointTrackerMiddleware>();
}

IPBlacklistMiddleware

Blocks requests from IPs flagged for malicious activity.

Example Integration
public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware<IPBlacklistMiddleware>();
}
Get Blocked IPs Log
var logs = IPBlacklistMiddleware.GetLogs();
foreach (var log in logs)
{
    Console.WriteLine(log.message);
}

EndpointAccessMiddleware

Monitors endpoint activity and performs memory cleanup if endpoints remain idle.

Example Integration
public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware<EndpointAccessMiddleware>(TimeSpan.FromMinutes(5), true);
}

Filters

APIKeyAttribute

Validates requests with an API key stored in environment variables.

Example Usage

Apply the attribute to a controller or action:

[APIKey]
[ApiController]
[Route("[controller]")]
public class SecureController : ControllerBase
{
    [HttpGet]
    public IActionResult GetData()
    {
        return Ok("Secure Data Accessed");
    }
}
Environment Variables Required
  • API_KEY: Your API key.
  • API_KEY_NAME: Header name for the API key.

Security Features

IP Blocking

IP addresses are automatically blocked after exceeding the threshold for unauthorized attempts.

IPBlacklist.AddBannedIP("192.168.1.1", "Repeated unauthorized attempts.");

Logging

All middleware components use the logging system provided by MandalaConsulting.Logging.

Add Logs

IPBlacklistMiddleware.AddLog(LogMessage.Warning("Suspicious activity detected."));

Retrieve Logs

var logs = IPBlacklistMiddleware.GetLogs();
foreach (var log in logs)
{
    Console.WriteLine(log.message);
}

Clear Logs

IPBlacklistMiddleware.ClearLogs();

Requirements

  • .NET Core 3.1 or later
  • ASP.NET Core

License

This project is copyrighted © 2024 Mandala Consulting, LLC.
All Rights Reserved.


Author

Created by Alexander Fields
For inquiries, please contact Mandala Consulting.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.