This is an easy way to do this using the state of the application, as it applies to all users and sessions accessing your application.
In the new RequestManager.cs file
public static class RequestManager
{
public static void Validate(HttpContext context)
{
if (context.Application["Blocklist"] == null)
context.Application.Add("Blocklist", new Dictionary<string, DateTime>());
Dictionary<string, DateTime> blocklist = context.Application["Blocklist"] as Dictionary<string, DateTime>;
if (blocklist.ContainsKey(context.Request.UserHostAddress))
{
DateTime lastRequest = blocklist[context.Request.UserHostAddress];
if (DateTime.Now.Subtract(lastRequest).TotalMilliseconds < 1000)
{
context.Response.Write(string.Format("You'll have to wait for {0} milliseconds until next request", 1000 - DateTime.Now.Subtract(lastRequest).TotalMilliseconds));
context.Response.End();
}
else
{
blocklist[context.Request.UserHostAddress] = DateTime.Now;
}
}
else
{
blocklist.Add(context.Request.UserHostAddress, DateTime.Now);
}
}
}
In your Global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
RequestManager.Validate(HttpContext.Current);
}
,