ApiRateLimiting 1.1.0
dotnet add package ApiRateLimiting --version 1.1.0
NuGet\Install-Package ApiRateLimiting -Version 1.1.0
<PackageReference Include="ApiRateLimiting" Version="1.1.0" />
paket add ApiRateLimiting --version 1.1.0
#r "nuget: ApiRateLimiting, 1.1.0"
// Install ApiRateLimiting as a Cake Addin #addin nuget:?package=ApiRateLimiting&version=1.1.0 // Install ApiRateLimiting as a Cake Tool #tool nuget:?package=ApiRateLimiting&version=1.1.0
API Rate Limiting Package
This is a simple API rate limiting middleware and service that allows you to limit the number of requests made by users or IP addresses within a specified time period.
Features
- IP-based or User-based rate limiting: Supports rate limiting based on user identity or IP address.
- Customizable rules: Define custom rules for rate limiting, such as requests per minute, hour, or day.
- Memory-based storage: Uses an in-memory cache to track requests per user/IP.
- Configurable: Configure the rate limits and time periods via JSON configuration or programmatically.
- Extensible: Easily extend the rate limiting logic or add custom features to fit your needs.
Installation
To install the package, use the dotnet add package ApiRateLimiting --version 1.1.0
.
Alternatively, you can download it from the NuGet Gallery.
Key Steps:
Create a blank project using the ASP.NET web api.
Choose .NET 8.0 for the project.
Install the ApiRateLimiting from NuGet packages.
Install-Package ApiRateLimiting -Version 1.1.0
Add code in the program.cs file:
Add namespace
using ApiRateLimiting;
.Add
builder.Services.Configure<IpRateLimitOptions>(builder.Configuration.GetSection("IpRateLimiting")); builder.Services.AddApiRateLimiting();
- Add
app.UseMiddleware<RateLimitingMiddleware>();
.
Update the application settings with the following configurations:
"IpRateLimiting": {
"GeneralRules": [
{
"Endpoint": "WeatherForecast/weatherforecast", //Set multiple endpoint
"Period": "2m", //Set period day for d ,hour for h,min for m
"Limit": 1 // Set limit
},
{
"Endpoint": "WeatherForecast/weatherforecast/v1",
"Period": "2m",
"Limit": 2
},
{
"Endpoint": "WeatherForecast/weatherforecast/v2",
"Period": "2m",
"Limit": 3
}
]
}
- Run the project to complete the setup.
How It Works
- The rate limiting service tracks requests based on the user or IP address and stores the data in memory.
- The CustomRateLimitService checks if the number of requests exceeds the defined limit within the specified period (e.g., 100 requests per hour).
- If the limit is exceeded, the RateLimitFilter blocks the request and returns a 429 Too Many Requests response with rate limiting details.
Product | Versions 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. |
-
net8.0
- DynamicFormBuilder (>= 1.1.0)
- EntityFramework (>= 6.5.1)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Caching.Memory (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.