AspNetCoreRateLimit icon indicating copy to clipboard operation
AspNetCoreRateLimit copied to clipboard

Cannot use this in Azure App Service as App Service Configuration requires all settings to be string type

Open chandde opened this issue 5 years ago • 2 comments

When I locally tested it and it worked pretty well, however, I have an Asp.Net Core App Service running in Azure, I use App Service Configuration to set the configs, there is a requirement that all settings key and value must be string type (i.e. quoted, here's a link to Azure Docs https://docs.microsoft.com/en-us/azure/app-service/configure-common), however package uses a few boolean and integer setting values, how should I make it work? see below snapshot, is there a way to work around this?

Do I have to load the string version of config, then build another config with manual conversion ("true" -> true, "20" -> 20) then feed the component?

image

"IpRateLimiting": {
  "EnableEndpointRateLimiting": true, <- non string
  "StackBlockedRequests": false, <- non string
  "RealIPHeader": "X-Real-IP",
  "ClientIdHeader": "X-ClientId",
  "HttpStatusCode": 429, <- non string
  "GeneralRules": [
    {
      "Endpoint": "*",
      "Period": "10m",
      "Limit": 60 <- non string
    },
    {
      "Endpoint": "*:/sendemail",
      "Period": "5m",
      "Limit": 5 <- non string
    }
  ]
}

chandde avatar Oct 14 '20 22:10 chandde

I resolved this issue by converting the entire IpRateLimiting configuration into a json, and put the json string in app settings (or Azure App Service Configuration),

{
    "otherconfigurations": "othervalues",
    "IpRateLimiting": "{\"EnableEndpointRateLimiting\":true,\"StackBlockedRequests\":false,\"RealIPHeader\":\"X-Real-IP\",\"ClientIdHeader\":\"X-ClientId\",\"HttpStatusCode\":429,\"GeneralRules\":[{\"Endpoint\":\"*\",\"Period\":\"10m\",\"Limit\":60},{\"Endpoint\":\"*:/sendemail\",\"Period\":\"5m\",\"Limit\":5}]}"
  }
}

At runtime, read the configuration value string, build a new configuration by the json stream

var ipRateLimitingStr = config.GetSection("IpRateLimiting");
var ipRateLimitingConfiguration = new ConfigurationBuilder().AddJsonStream(new MemoryStream(Encoding.ASCII.GetBytes(ipRateLimitingStr.Value))).Build();
services.Configure<IpRateLimitOptions>(ipRateLimitingConfiguration);

chandde avatar Oct 14 '20 23:10 chandde

I use Azure App Configuration and I can set these values like "True" and they get converted into an actual boolean value when IOptionsSnapshot<IPRateLimitOptions> or IOptionsSnapshot<ClientRateLimitOptions> is used. You can test this yourself by injecting one of those into your controller or service and observing the property in your debugger.

image

I would assume it would be the same with Azure App Service Configuration since it's just another configuration source.

aeslinger0 avatar Dec 13 '23 23:12 aeslinger0