RateLimit.Framework
1.0.3
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package RateLimit.Framework --version 1.0.3
NuGet\Install-Package RateLimit.Framework -Version 1.0.3
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="RateLimit.Framework" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RateLimit.Framework --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: RateLimit.Framework, 1.0.3"
#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.
// Install RateLimit.Framework as a Cake Addin #addin nuget:?package=RateLimit.Framework&version=1.0.3 // Install RateLimit.Framework as a Cake Tool #tool nuget:?package=RateLimit.Framework&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Author: Khoi Tran
Package: RateLimit.Framwork
- The
lightweight
package
provides a middleware service to protect your application by limiting the request rate. - Suport
old
.NET Framework
project - Support three way caching:
Redis
,MemoryCache
,System.Web.Caching.Cache
- How it work: If an
IpAddress
(can be config using withCookieName
) contains the number of requests which cross over theMaxRequestPerPeriod
, it will be blocked by returning aTooManyRequest(429)
(config byBlockMessage
) within the blocking time (config byBlockTimeSecond
)
Usage
Index.cshtml
: make simple ajax call to HomeController
$.ajax({
url: "/Home/GetItem/1234",
type: "GET",
async: false,
datatype: "json",
data: {
param1: "param1 data",
param2: "param2 data",
param3: "param3 data"
},
success: function (data) {
console.log(data)
},
error: function (err) {
console.log(err)
}
})
HomeController.cs
: config RateLimitAttribute forGetReport
action
using Newtonsoft.Json;
using System.Web.Mvc;
using RateLimit; // use RateLimit package
namespace WebMVC.Controllers
{
public class HomeController : Controller
{
/* Config for every request come to this action */
[HttpGet]
[Route("GetItem/{id?}")]
[RateLimit(PeriodTime = 10
, MaxRequestPerPeriod = 1
, CookieName = "UserId"
, QueryParams = "param1, param2" // or "param2, param1" not need to do in order
, RouteParams = "id" // same here, you not need to do in order if you have many Route param
, BlockTime = 10
, BlockMessage = "You are making too many request")]
public ActionResult GetItem(string id, string param1, string param2)
{
return new JsonResult() {
Data = new
{
now = System.DateTime.Now.ToString("dd HH:mm:ss.ff"),
id,
param1,
param2
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
}
}
Advance
Global.asax.cs
: custom regis global RateLimit configuration
/// ...
using RateLimit; // use RateLimit package
using RateLimit.Configs; // use RateLimit package configuration
namespace WebMVC
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
/// ...
/// ...
/* Full global config */
RateLimitContext.RegisterConfig(
new RateLimitConfig(
/* Global config for request rate */
rateConfig: new RequestRateConfig
{
BlockMessage = "You are making too many request, please try after 60 second",
BlockTime = 60,
CookieName = "ASP.NET_SessionId",
MaxRequestPerPeriod = 1,
PeriodTime = 10,
},
/* Cache config, default is new HttpRuntimeConfig() */
cacheConfig: new RedisConfig()
{
CacheTimeSecond = 3600,
ConnectTimeoutSecond = 5,
DatabaseIndex = 0,
EndPoints = "localhost:6379"
}
)
);
}
}
}
HomeController.cs
: Use default global rate configuration
/// ...
using RateLimit; // use RateLimit package
namespace WebMVC.Controllers
{
public class HomeController : Controller
{
/* Use default global rate configuration */
[RateLimit]
public string GetReport(string param1)
{
return param1;
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.5
- Microsoft.AspNet.Mvc (>= 5.0.0)
- Microsoft.AspNet.Razor (>= 3.2.9)
- Microsoft.AspNet.WebPages (>= 3.2.0)
- Newtonsoft.Json (>= 9.0.1)
- StackExchange.Redis (>= 1.2.6)
- System.Runtime.Caching (>= 4.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Add QueryParams, RouteParmas property