CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Karl Seguin

.NET From Ottawa, Ontario

Captcha Control

I dug something else up while doing some cleaning : a captcha control. The core functionality is taken from this codeproject article. I've gone ahead and wrapped it in a custom server control as well as taking advantage of HttpHandlers.

This was actually incorporated into a bigger library, so I had to rework some stuff to make it more stand-alonish. I also noticed that I could have exposed more properties to make it more flexible - oh well.

Since it makes use of HttpHandlers and custom server controls, I think it really highlights some of the best tools ASP.NET offers when it comes to reuse. Sure it might seem like a lot of code, but it's contained set of code which is ready to be reused in any projects - write once, reuse often. (That said, I'm not actually including a .dll file (just the source), because I don't want to give anyone the impression that it's a solid 3rd party control.)

To set it up, add the 4 .cs files to your App_Code folder (or a separate project). Add these line to your web.config's system.web element:
<httpHandlers>
  <add type="Fuel.Web.CaptchaHandler, App_Code" verb="GET" path="captcha.ashx" />
 </httpHandlers>

And then Register the namespace:
<%@ Register Assembly="App_Code" Namespace="Fuel.Web" TagName="Fuel" %>

All you need to do then is use the control:
<Fuel:Captcha runat="Server" />

If you aren't using 2.0, or aren't placing these in your App_Code folder, simply replace "App_Code" with the name of the .dll it'll get compiled to.

Finally, if you are using 2.0, don't forget that you can register controls site-wide now via:
<pages>
      <controls>
        <add assembly="App_Code" namespace="Fuel.Web" tagPrefix="Fuel"/>
      </controls>
</pages>


in system.web of the web.config

Download it here



Published Jul 13 2006, 09:14 AM by karl
Filed under:

Comments

Marc Brooks said:

Thanks for this, nicely done.  One tiny note, you've mispelled Courier (font name) in the CaptchaHandler.cs file so Windows will use whatever font it wants :)
# July 13, 2006 11:27 AM

Jake Good said:

You could always look into:

http://www.hotcaptcha.com (SFW)

hehe
# July 13, 2006 11:32 AM

Jason Haley said:

# July 13, 2006 11:44 PM

Peter Kellner said:

I haven't done a lot with custom server controls, but I think for this to work with asp.net 2.0 you need to change the register line to this:

<%@ Register Namespace="Fuel.Web" TagPrefix="Fuel" %>

# August 5, 2006 9:45 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications