Shredding, coding, arguing etc.

ASP .NET 2.0 Captcha Control

(Compatible with Telerik RadAjax and Peter Blum validators)

7 June 2008

I needed a simple ASP .NET Captcha control and this one on The Code Project appeared to do everything that I wanted. However, it didn't work very well with Telerik's RadAjax or Peter Blum's Professional Validation And More, so I rearranged a few things. You may find it also behaves better with regular ASP .NET "Atlas" Ajax. Please let me know if it does.

Download ASP .NET 2.0 source files

Sorry I can't remember the exact details of what I changed, but I've been using it successfully for a while now and haven't experienced any problems. Let me know if it gives you any trouble or if you can make any improvements.

Installation

Please refer to the original instructions.

Briefly:

  1. Copy the dll to your bin directory, or add a reference to it in your web project.
  2. Reference it from your web.config like this:
    <system.web> <pages> <add tagPrefix="my"> namespace="WebControlCaptcha" assembly="WebControlCaptcha" /> </pages>
  3. Add an html handler:
    <system.web> <httpHandlers> <add verb="GET"> path="CaptchaImage.aspx" type="WebControlCaptcha.CaptchaImageHandler, WebControlCaptcha" /> </httpHandlers>
    (Note that the page CaptchaImage.aspx doesn't actually exist!)
  4. Now you should be able to add it to your page like this:
    <my:CaptchaControl runat="server" />

You should be able to attach a custom validator to it and AJAX post backs will work correctly.

Update: 17 Sep 2008

Achilles has had some difficulty using this control and has had a bit of a cry about it on his blog :-).

The trouble he was having with my solution occurs when the CaptchaControl itself is part of a RadAjax update, for example if the CaptchaControl is inside a RadAjaxPanel. Sure enough, when I tried it out, the control didn't work properly.

He has posted some new code, however his solution has the same problem that I was originally trying to get around! If an ajax postback occurs elsewhere on the page, perhaps in a completely unrelated place, then the CaptchaControl will not work correctly.

So anyway, you the reader now have three choices to choose from:

  1. CodingHorror's original code which never works with RadAjax.
  2. Achille's code that works if the CaptchaControl is inside a RadAjax update but doesn't work if an ajax postback occurs elsewhere on the page.
  3. Mumblepuppy's solution which works if a RadAjax update occurs elsewhere on the page, but not if the CaptchaControl is part of a RadAjax update itself.

Perhaps someone else wants to have a shot at fixing it?

Comments

Man! You had just ruined the Control! It's always validated! I had to rewrite the controls. Why do you check the existence of RadAjax? You don't care whether the Captcha is inside or outside the RadAjaxPanel! I'm gonna revise the code again. I just made it work for me but there are some improvements I plan to do. I'll put it in my weblog and will include your credit too ;) http:[slash][slash]blog[dot]saffarionline[dot]NET
Achilles
09 Sep 2008 16:03
 
   
Stupidity filter

Unfortunately, the internet is rife with automaton zombies that fill up blog threads with rubbish. There are also computer scripts that do this too. Please answer the following question.

Waht is the nmae of the plnaet clsoset to the cneter of the sloar ssyetm?    
  •  
  • del.icio.us  del.icio.us
  • reddit  reddit
  • StumbleUpon  StumbleUpon