An ASP.NET CAPTCHA Server Control

October 1, 2004

A few days ago, I found a really cool CAPTCHA ASP.NET code sample. I converted it to VB.NET and repackaged it as a full blown ASP.NET server control:

screenshot of ASP.NET CAPTCHA server control

It's as simple as I could make it: a total drag and drop, set the (three) properties and forget it implementation. The only tricky part was dealing with the dynamically generated image. You will have to add this HttpHandler section to your web.config file when you use the CaptchaControl

    <httpHandlers>
        <add verb="*" path="CaptchaImage.aspx" 
            type="WebControlCaptcha.CaptchaImageStream, WebControlCaptcha" />
    </httpHandlers>

You can download the zipped VS.NET 2003 solution from my CodeProject article if you're interested. There are only two projects in the solution; an ultra simple demo website and the control library itself.

I should also mention that I tested this CAPTCHA against OCR software, specifically OmniPage Pro 14. It does suprisingly well for a couple key reasons-- low contrast, and all the characters are warped. It's possible to defeat it, but it's definitely not a trivial "drag to OCR window" situation. My kudos to BrainJar for coming up with this simple yet effective CAPTCHA.

Posted by Jeff Atwood
35 Comments

Very nice!

Darrell on October 1, 2004 10:27 AM

I developed one of these myself (in C#, however) and have it working on my site (link above).

brady gaster on October 4, 2004 10:07 AM

Brady, very cool, although I suspect that is very OCR-able due to the high contrast and lack of warping. Probably doesn't matter since the effort bar has already been raised, but for the record ;)

I made another small improvement: I am using a HttpHandler instead of a HttpModule. That way I don't have to test every request for a page name.. the handler only fires for that one page.

Jeff Atwood on October 4, 2004 12:25 PM

There's also a great MSDN article on this topic here:

http://msdn.microsoft.com/asp.net/using/building/web/default.aspx?pull=/library/en-us/dnaspp/html/hip_aspnet.asp

Jeff Atwood on October 10, 2004 5:45 AM

The codeproject link doesn't work any more. Is there an updated Url?

mike h on March 29, 2005 8:49 AM

Sure, that's no problem. Scott Hanselman already included the CAPTCHA in his release of dasBlog as well.

Jeff Atwood on March 29, 2005 9:35 AM

Looks like the MSDN URL has changed; I suspect it refers to this (very good) article:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/hip_aspnet.asp

This is based upon the .NET 1.x framework.

Regards, Matt

Matt on September 14, 2006 12:45 PM

For me, I have found that the control doesn't work when it resides on a page that uses master pages. When the control is on a basic *.aspx page, it works fine. However, the site I want to use this with uses master pages. Can you help me to understand why that may be?? In laymen's terms as well. I am not a code guru.

Kemrex on November 10, 2007 11:24 AM

I have solved my problem. There was a CSS conflict with my stylesheet and the styles applied to the DIV/SPAN elements within the control itself. It was also only an IE 6 bug as well. I stripped out all of the styles from your control and it worked with my stylesheet!

Kemrex on November 11, 2007 8:47 AM

Very nice! and very helpful. I shall use it in my .net projects.
thanks

Humayun Shabbir on November 16, 2007 4:12 AM

Nice work, Jefferson.

Why did you choose not to use it on codinghorror.com?

gunther on January 2, 2008 10:16 AM

The 2.0 project download on The Code Project is missing the CaptchaImage.aspx file.

9key on January 16, 2008 2:13 AM

12w21w21

www on January 18, 2008 7:43 AM

One question and one comment...

First, great control! You saved me a ton of time. =)

Now, for the question...

I'm using your controlw ith a Template version of the Login control. Unfortunately, the control appears to not have any events that fire upon failure. I would to display a simple error to my user when they make a typo in the CAPTCHA.

Any thoughts or recommendations?

Ryan on January 23, 2008 3:23 AM

hk

hjk on February 25, 2008 3:08 AM

dfhjkdhfjkdjkfdjkfjkhfkdf
.dfdhfjdjkf
f'djfhdjfjkd

dev on March 1, 2008 4:27 AM

dev astha shefalii arshi pawan

shefalii on March 1, 2008 4:29 AM

Hi,

It is a great control and i am using it in a project.

The only problem is sometime the CAPTCHA image can not be displayed and i have to re-start IIS.

Please help.

Thanks

Yu on March 5, 2008 3:46 AM

cvzxvcxbv

xvcv on March 13, 2008 8:40 AM

anyone happen to have a C# version?
Tx..

matt p on April 21, 2008 11:36 AM

where is source code of this control...
and runtime files...

feroz on April 28, 2008 1:02 PM

Where's the source code? The link is broken!

9key on May 12, 2008 3:11 AM

There is an AJAX compatible version here: http://www.centrifugalbumblepuppy.com/posts/telerik-ajax-compatible-captcha-control.aspx

ajaxversion on June 9, 2008 6:00 AM

dfgsdfg

ghj on July 30, 2008 1:23 PM

dfg

dgd on September 3, 2008 1:24 PM

sddsf

sdfds on October 7, 2008 2:24 AM

Hi, thanks for the article; in fact I used your render idea to integrate it into a fully featured control http://www.sharppieces.com/free-aspnet-controls/CaptchaImage.aspx. The control is open source on codeplex, check it out if you have time.

Alex Botez on October 20, 2008 12:43 PM

Test

Test on February 23, 2009 4:14 AM

hola a todos buen blog
Entrar a esteblog es interesante

JUAN on March 11, 2009 9:23 AM

this is a buen blog here a href=http://www.google.com.pe class=title-link for looking

Ernesto on March 11, 2009 9:24 AM

For windows vista, you have to add:

add name=CaptchaImage path=CaptchaImage.aspx verb=GET type=WebControlCaptcha.CaptchaImageHandler resourceType=Unspecified preCondition=integratedMode /

in handlers section.

Regards,
Asang Dani

Asang Dani on April 9, 2009 6:38 AM

Implementation of Custom web control to display CAPTCHA image can be found here:

http://etechplanet.com/post/2009/05/11/Create-CAPTCHA-Image-using-C-and-ASPNet.aspx

Avi on May 11, 2009 10:39 AM

test

Anonymous on June 8, 2009 7:59 AM

Cool stuff, thanks. I'm actually thinking of wrapping this up in a DotNetNuke module, if it's ok with you.

Burton on February 6, 2010 9:30 PM

If you want a simple Captcha in asp.net, check this article: http://www.massimilianobianchi.info/max/20art_A-simple-CAPTCHA-with-ASP-NET.aspx

Massimiliano Bianchi on April 20, 2011 12:49 AM

The comments to this entry are closed.