ApiRateLimitUserId.AspNetCore.AltairCA 1.0.3.7

dotnet add package ApiRateLimitUserId.AspNetCore.AltairCA --version 1.0.3.7                
NuGet\Install-Package ApiRateLimitUserId.AspNetCore.AltairCA -Version 1.0.3.7                
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="ApiRateLimitUserId.AspNetCore.AltairCA" Version="1.0.3.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ApiRateLimitUserId.AspNetCore.AltairCA --version 1.0.3.7                
#r "nuget: ApiRateLimitUserId.AspNetCore.AltairCA, 1.0.3.7"                
#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 ApiRateLimitUserId.AspNetCore.AltairCA as a Cake Addin
#addin nuget:?package=ApiRateLimitUserId.AspNetCore.AltairCA&version=1.0.3.7

// Install ApiRateLimitUserId.AspNetCore.AltairCA as a Cake Tool
#tool nuget:?package=ApiRateLimitUserId.AspNetCore.AltairCA&version=1.0.3.7                

IpRateLimiter.AspNetCore.AltairCA

Build Status

ApiRateLimitUserId.AspNetCore.AltairCA is an request limiting solution by looking at the logged in userid.

Inspired by AspNetCoreRateLimit repo link

ApiRateLimitUserId.AspNetCore.AltairCA targets netstandard2.0. The package has following dependencies

<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.1.3" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />

setup

NuGet install:

Install-Package ApiRateLimitUserId.AspNetCore.AltairCA

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  services.AddMemoryCache();
  services.AddHttpContextAccessor();
  services.AddAPIRateLimiterUserId(options =>
  {
      options.GlobalRateLimit = 10;
      options.GlobalSpan = TimeSpan.FromMinutes(30);
      options.ExcludeList = new List<string>
      {
          "UserID"
      };
      options.UserIdClaim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier";
  }).AddMemoryCache();

Default options for ApiRateLimitUserId

public class APIRateLimiterUserIdOptions
    {
        public string UserIdClaim { get; set; }
        public List<string> ExcludeList { get; set; }
        public int GlobalRateLimit { get; set; } = 1000;
        public TimeSpan GlobalSpan { get; set; } = TimeSpan.FromMinutes(30);
        public int StatusCode { get; set; } = 429;
        public object LimitReachedResponse = new {detail = "Quota exceeded. Maximum allowed: {0} per {1}. Please try again in {2} second(s)." };
        public string CachePrefix { get; set; } = "AltairCAAPIRATELIMIT";

    }

{0} is max limit, {1} is period in seconds, {2} when the quota get resets in seconds

Redis Provider

Above example uses memerycache as the provider for the data, but I have implemented a redis storage provider as well

Refer here to lean more about Redis Client, that I have used

services.AddAPIRateLimiterUserId(options =>
                {
                    options.GlobalRateLimit = 10;
                    options.GlobalSpan = TimeSpan.FromMinutes(30);
                    options.ExcludeList = new List<string>
                    {
                        "127.0.0.1", "192.168.0.0/24"
                    };
                    options.UserIdClaim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier";
                }).AddRedisCacheProvider(() => "127.0.0.1:6379");

Using it in a controller

    [Route("api/[controller]")]
    [ApiController]
    [IpRateLimitHttp]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }
        [IpRateLimitHttp(10*60,2,"group1" )]
        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }
        [IpRateLimitHttp(10*60,2,"group1" )]
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }
    }

You can apply the filter attribute at the top of the controller class. It will apply the rule for all of the endpoints that defined in the controller or you can put the attribute at the endpoint level. If you put the attribute at the class level and the endpoint level it will work as a AND operator.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3.7 519 3/22/2021
1.0.3.6 475 3/22/2021
1.0.3.5 415 3/22/2021
1.0.3.4 441 3/19/2021
1.0.3.3 451 3/17/2021
1.0.3.2 468 3/17/2021
1.0.3.1 421 3/17/2021
1.0.3 429 3/17/2021
1.0.2 427 3/7/2021
1.0.1 441 3/6/2021
1.0.0 426 3/5/2021