【新增】新增客户端IP限流模块。

This commit is contained in:
JianWeie
2022-05-20 14:48:06 +08:00
parent 05e2ff05bb
commit f973854d98
9 changed files with 228 additions and 1 deletions

View File

@@ -4,6 +4,10 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspNetCoreRateLimit" Version="4.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CoreCms.Net.Auth\CoreCms.Net.Auth.csproj" />
<ProjectReference Include="..\CoreCms.Net.Configuration\CoreCms.Net.Configuration.csproj" />

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AspNetCoreRateLimit;
using CoreCms.Net.Configuration;
using CoreCms.Net.Loging;
using Hangfire.Logging;
using Microsoft.AspNetCore.Builder;
using NLog;
using LogLevel = NLog.LogLevel;
namespace CoreCms.Net.Middlewares
{
/// <summary>
/// 限制 ip 流量
/// </summary>
public static class IpLimitMiddleware
{
public static void UseIpLimitMiddle(this IApplicationBuilder app)
{
if (app == null) throw new ArgumentNullException(nameof(app));
try
{
if (AppSettingsConstVars.MiddlewareIpRateLimitEnabled)
{
app.UseIpRateLimiting();
}
}
catch (Exception e)
{
NLogUtil.WriteFileLog(LogLevel.Error, LogType.ApiRequest, "全局捕获异常", "限制ip流量异常", e);
throw;
}
}
}
}