Created
May 10, 2019 00:34
-
-
Save cristipufu/66f3f0bc24c86c922f85f0e279422daf to your computer and use it in GitHub Desktop.
Custom Rate Limit Incrementer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Options; | |
namespace AspNetCoreRateLimit.Demo | |
{ | |
public class MPRateLimitConfiguration : RateLimitConfiguration | |
{ | |
public MPRateLimitConfiguration( | |
IHttpContextAccessor httpContextAccessor, | |
IOptions<IpRateLimitOptions> ipOptions, | |
IOptions<ClientRateLimitOptions> clientOptions) | |
: base(httpContextAccessor, ipOptions, clientOptions) | |
{ | |
} | |
public override Func<long> RateIncrementer => () => | |
{ | |
long value = 0; | |
var httpContext = HttpContextAccessor.HttpContext; | |
if (httpContext.Request.Headers.TryGetValue("X-UIPATH-Metadata", out var values)) | |
{ | |
// values.First() is actually a json if i remember correctly | |
// so it first needs deserialization and then actual value extraction | |
if (long.TryParse(values.First(), out value)) | |
{ | |
return value; | |
} | |
} | |
return value; | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddSingleton<IRateLimitConfiguration, MPRateLimitConfiguration>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment