I'm building an ASP.NET MVC site where I want to limit how often authenticated users can use some functions of the site.
Although I understand how rate-limiting works fundamentally, I can't visualize how to implement it programatically without creating a major code smell.
Can you point me towards a simple yet powerful solution for approaching such a problem, with C# sample code?
If it matters, all of these functions are currently expressed as Actions that only accept HTTP POST
. I may eventually want to implement rate-limiting for HTTP GET
functions as well, so I'm looking for a solution that works for all such circumstances.
Have a look at Jarrod's answer on how they do this on SO.
StackOverflow MVC Throttling
Some example code as well as explanation on how it works.
If you are using IIS 7 you could take a look at the Dynamic IP Restrictions Extension. Another possibility is to implement this as an action filter:
And then decorate the action that needs to be limited: