Click here to Skip to main content
15,885,366 members
Articles / Web Development / ASP.NET
Article

Use a floodgate class to block multiple posts

Rate me:
Please Sign up or sign in to vote.
4.58/5 (6 votes)
27 Sep 2005CPOL2 min read 55.5K   231   40   19
A simple class to block repeated attempts to submit comments or logins.

Introduction

Here's something that I'd like to share with you. I was developing a website and wanted to limit the number of operations per second that a client is allowed to post. For example, if a visitor posts a comment within 10 seconds of his previous post, it's very unlikely that the post contributed by the person is something useful. Most probably, that post was somehow automated.

Solution

The class that I have written is actually very simple to use. For example if you have an event handler that implements the post of a comment, your code may look something like this:

C#
private void Submit_click(object sender, EventArgs args)
{
    if(Page.IsValid)
    {
        //Your code to persist the details in the database.
    }
}

Now, to add our flood gate, change the code so that it looks something like this:

C#
private static FloodGate _submitFlooding;

private void Submit_click(object sender, EventArgs args)
{
    if(Page.IsValid)
    {
         if(_submitFlooding == null)
    {
         _submitFlooding = new FloodGate("Post Comment", 
                                    TimeSpan.FromSeconds(10))
    }
     _submitFlooding.Assert();
 
    //Your code to persist the details in the database.
    }
}

First, I declare a static reference to a FloodGate class. This makes sure that my floodgate is kept in memory across all the requests that are made to this application.

When somebody clicks on the Submit button, an instance of a FloodGate is assigned. It takes two parameters: the name of the gate, and the minimum delay between each operation.

Whenever somebody (with a certain client IP address) clicks the button within 10 seconds, a FloodGateException is thrown, thereby effectively aborting the operation.

To make it more user friendly, you can catch the exception and show a message box.

The code

It works as follows:

  1. The class maintains a Hashtable of client IP's.
  2. Whenever the Assert() method is called, a check is performed to see if the current client IP (by using HttpContext.Current.Request.UserHostAddress) is already on the list.
  3. If it is, the Hashtable entries contain the DateTime of the last operation. If that date is no longer than n seconds apart (configured by the constructor), a FloodGateException is thrown.
  4. If it isn't (or sufficient time has elapsed), the Hashtable is updated.

If you're interested in seeing the entire code, it is available for download at the top of the page.

The cool thing about this solution is that it is easily reusable in both code-behind and business classes, and that you can declare multiple floodgates for different operations. One of the downsides is that a reference to System.Web is required.

Improvements

The first thing that comes to mind: Clean up IP addresses that have dates older than the minimum waiting time. This will save precious memory.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Netherlands Netherlands
Rob has been a professional web developer since 1998, and is working with C# and ASP.Net since early 2002.

Most of the time, his focus is on creating a clean and simple solution to development problems. (Although that sometimes means that he needs to do some hard work to make somebody else's life more easier.).

Comments and Discussions

 
QuestionWhat about multiple users thru a shared ip address? Pin
david wendelken19-Nov-07 9:54
david wendelken19-Nov-07 9:54 
AnswerRe: What about multiple users thru a shared ip address? Pin
Rob van der Veer20-Nov-07 2:50
Rob van der Veer20-Nov-07 2:50 
Generalthanks Pin
Onur Ucak13-Apr-07 0:05
Onur Ucak13-Apr-07 0:05 
GeneralRe: thanks Pin
Rob van der Veer3-May-07 11:21
Rob van der Veer3-May-07 11:21 
GeneralNice Pin
Clickok18-Jan-07 4:36
Clickok18-Jan-07 4:36 
GeneralRe: Nice Pin
Rob van der Veer4-Mar-09 5:47
Rob van der Veer4-Mar-09 5:47 
GeneralWhat's the best way to handle the exception Pin
Gordon Moore26-Nov-05 10:23
Gordon Moore26-Nov-05 10:23 
GeneralRe: What's the best way to handle the exception Pin
Rob van der Veer27-Nov-05 2:30
Rob van der Veer27-Nov-05 2:30 
GeneralRe: What's the best way to handle the exception Pin
Gordon Moore27-Nov-05 3:38
Gordon Moore27-Nov-05 3:38 
GeneralRe: What's the best way to handle the exception Pin
Gordon Moore27-Nov-05 3:48
Gordon Moore27-Nov-05 3:48 
GeneralRe: What's the best way to handle the exception Pin
Gordon Moore27-Nov-05 4:04
Gordon Moore27-Nov-05 4:04 
GeneralRe: What's the best way to handle the exception Pin
Gordon Moore27-Nov-05 4:51
Gordon Moore27-Nov-05 4:51 
GeneralRe: What's the best way to handle the exception Pin
Gordon Moore3-Dec-05 23:21
Gordon Moore3-Dec-05 23:21 
GeneralRe: What's the best way to handle the exception Pin
Shyam K Pananghat3-Mar-09 22:54
Shyam K Pananghat3-Mar-09 22:54 
GeneralRe: What's the best way to handle the exception Pin
Shyam K Pananghat3-Mar-09 23:04
Shyam K Pananghat3-Mar-09 23:04 
GeneralLink doesn't work Pin
wickedtuner6-Oct-05 20:37
wickedtuner6-Oct-05 20:37 
GeneralRe: Link doesn't work Pin
Rob van der Veer9-Oct-05 10:10
Rob van der Veer9-Oct-05 10:10 
GeneralRe: Link doesn't work Pin
Rob van der Veer13-Oct-05 11:33
Rob van der Veer13-Oct-05 11:33 
GeneralRe: Link doesn't work Pin
J C Dinaker30-May-07 19:21
J C Dinaker30-May-07 19:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.