如何实现率限制在ASP。网络核心

利用速度限制在ASP。网络核心to prevent malicious attacks on your web application.

  • 在Facebook上分享
  • 在Twitter上分享
  • 分享在LinkedIn
  • 在Reddit分享
  • 通过电子邮件分享
  • 印刷资源
如何实现率限制在ASP。网络核心
皮特•史密斯(CC0)

在构建web应用程序时,您可能会想要控制用户请求的频率,防止恶意攻击。换句话说,您可能希望限制请求来自一个IP地址的数量在很短的时间跨度内减轻拒绝服务攻击。这个过程被称为率限制。

速度限制使您能够控制请求的数量,客户端可以使一个端点。在ASP。网络核心,you had to write a lot of boilerplate code to implement rate limiting. However, implementing rate limiting is easy in ASP.NET Core. In this article, we'll examine how we can work with rate limiting in ASP.NET Core.

创建一个ASP。网络核心APIproject

首先,让我们创建一个ASP。网络核心project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.NET Core project in Visual Studio.

  1. 启动Visual Studio IDE。
  2. 点击“创建新项目。”
  3. 在“创建新项目”窗口中,选择“ASP。网络核心Web Application” from the list of templates displayed.
  4. 单击Next。
  5. 在“配置您的新项目”窗口中,指定新项目的名称和位置。
  6. 单击Create。
  7. 在“创建新的ASP。网络核心Web Application” shown next, select .NET Core as the runtime and ASP.NET Core 2.2 (or later) from the drop-down list at the top.
  8. 选择“API”的项目模板创建一个新的ASP。网络核心API的应用程序。
  9. 确保复选框“启用码头工人支持”和“配置HTTPS”不像在这里我们不会使用这些功能。
  10. 确保身份验证设置为“无身份验证”是我们不会使用身份验证。
  11. 单击Create。

以下步骤应该创建一个新的ASP。在Visual Studio净核心API项目。我们将使用这个项目实施速度限制在本文的后续部分。

安装速度限制了中间件在ASP。网络核心

实施速度限制在ASP。网络核心,we will take advantage of a rate limiting middleware package for ASP.NET Core. You can use this middleware to set rate limits for various scenarios, such as allowing an IP or a range of IPs to make a specified number of calls within a specific time interval. The time interval can be per second, per minute, pern分钟,等等。

您已经创建了一个ASP。网络核心API项目in Visual Studio, the next step is to install the necessary NuGet packages. The package you will need to implement rate limiting is AspNetCoreRateLimit. You can install this package from the NuGet Package Manager inside Visual Studio, or by entering the following command in the .NET CLI.

dotnet添加包AspNetCoreRateLimit

你可以了解更多的AspNetCoreRateLimit包从GitHub库在这里

速率限制配置中间件在ASP。网络核心

现在AspNetCoreRateLimit包是安装在您的项目,编写下面的代码在ConfigureServices方法添加中间件请求-响应管道。

公共空间ConfigureServices (IServiceCollection服务)
{
.SetCompatibilityVersion services.AddMvc ()
(CompatibilityVersion.Version_2_2);
services.AddOptions ();
services.AddMemoryCache ();
services.Configure < IpRateLimitOptions >
(Configuration.GetSection (“IpRateLimit”));
services.AddSingleton < IIpPolicyStore,
MemoryCacheIpPolicyStore > ();
services.AddSingleton < IRateLimitCounterStore,
MemoryCacheRateLimitCounterStore > ();
services.AddSingleton < IRateLimitConfiguration,
RateLimitConfiguration > ();
services.AddHttpContextAccessor ();
}

注意使用部分名称IpRateLimit在上面所示的代码片段。本节在appsettings。json文件将包含元数据率限制。

接下来,编写以下代码的配置方法使用速率限制中间件。

公共空间配置(IApplicationBuilder应用,IHostingEnvironment env)
{
如果(env.IsDevelopment ())
{
app.UseDeveloperExceptionPage ();
}
app.UseIpRateLimiting ();
app.UseMvcWithDefaultRoute ();
}

最后,在appsettings编写下面的代码。json文件。注意指定的速率限制规则。速率限制规则包含一个端点,一段时间,和限制。

{
" IpRateLimit ": {
“EnableEndpointRateLimiting”:没错,
“StackBlockedRequests”:假的,
:“RealIPHeader X-Real-IP”,
:“ClientIdHeader X-ClientId”,
“HttpStatusCode”: 429年,
“GeneralRules”:(
{
“端点”:“*:/ api / *”,
“期”:“1米”,
“限制”:5
}
]
}
}

上面的速率限制规则将确保“/ api”的端点URI将限于五请求任何一分钟时间。

测试速率限制在你的ASP。网络核心API

运行应用程序,按下Ctrl和F5键在一起。当你运行应用程序时,ValuesController的Get方法(这是默认控制器为您创建Visual Studio)将会被执行,并且将显示以下消息。

value1

value2

现在五次刷新web页面,您应该能看到以下消息。

aspnetcoreratelimit马克斯调用 IDG

的最大数量调用你的ASP。网络核心API。

为了便于说明,我将速率限制设置为5。自然,你应该改变速率限制根据应用程序的需要。可以存储率限制在数据库或缓存内存。AspNetCoreRateLimit率限制中间件允许您在运行时更新率限制。

版权©2019 IDG通信公司。

奖2023年信息世界的技术。现在打开条目!