环境

  • .NET SDK 2.2

参考

步骤

  1. 在项目内使用NuGet添加AspNetCoreRateLimit包
    • 注意.NET Core版本必须为2.2及以上,2.1版本安装会报错
  2. 编辑Startup.cs,DI组件,并启用中间件
    //using ...
    using AspNetCoreRateLimit;
    
    public void ConfigureServices(IServiceCollection services) {
        // ...
        services.AddOptions();
        services.AddMemoryCache();
    
        services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
        services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
    
        services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
        services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
    
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
        // ...   
        app.UseIpRateLimiting(); //注意在UseMvc()前注册
    
        app.UseMvc();
    }
  3. 编辑appsettings.json,设置具体的IP限制
    {
        ...
        "IpRateLimiting": {
            "EnableEndpointRateLimiting": true, //此字段设定为false则全局有效,会限制所有*的规则。例如:如果频率限制到每秒5次,则会纪录向任何端点的任何HTTP请求
                                                //这里设置为true可以对每个端点分别设置请求频率
            "StackBlockedRequests": false,
            "RealIpHeader": "X-Real-IP",
            "ClientIdHeader": "X-ClientId",
            "HttpStatusCode": 429,
            "GeneralRules": [
                { //设置hello路径的请求频率为每30秒3次请求,"*:"代表任何HTTP VERB,"/*"代表其后的参数
                    "Endpoint": "*:/hello/*",
                    "Period": "30s",
                    "Limit": 3
                }
            ]
        }
    }
    • 可以配置更多功能,例如白名单,更改计数方式,自定义返回内容等等,详细信息可以查看GIthub WIki
  4. 配置Action的路由
    namespace IP_restrict_test.Controllers {
        public class HomeController: Controller {
            //...
    
            [Route("/hello/")]
            public IActionResult Index() {
                return View();
            }
            
            //...
        }
    }
  5. 生成并运行,测试频率限制功能
    • 可以看到,连续打开或者刷新页面3次后,第4次打开页面会提示访问频率过快。

Reference

Offical guide: How to remove the .NET Core Runtime and SDK

Steps

  1. Use
    dotnet --info

    command to check the dotnet sdk version you are currently using, it will list all the installed dotnet SDKs in your computer.

    • You can also use
      dotnet --list-sdks

      and

      dotnet --list-runtimes

      to list sdks and runtimes separately.

  2. Once you decided which version to be removed, execute command
    #remove sdk
    sudo rm -rf /usr/local/share/dotnet/sdk/[version number here]
    #remove .NET Core runtime
    sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.NETCore.App/[version number here]
    #remove ASP.NET Core runtime
    sudo rm -rf /usr/local/share/dotnet/shared/Microsoft.AspNetCore.App/[version number here]
    #I actually don't know what is in this directory
    sudo rm -rf /usr/local/share/dotnet/host/fxr/[version number here]

    to remove the sepecific SDK or runtime.