
Introduction
This article is continuing an existing article found on code project and written by Brainjar.
You'll find this project here. He was proposing a good strategy to prevent web forms from spammers.
Brainjar says
CAPTCHA stands for "completely automated public Turing test to tell computers and humans apart." What it means is, a program that can tell humans from machines using some type of generated test. A test most people can easily pass but a computer program cannot.
You've probably encountered such tests when signing up for an online email or forum account. The form might include an image of distorted text, like that seen above, which you are required to type into a text field.
The idea is to prevent spammers from using web bots to automatically post form data in order to create email accounts (for sending spam) or to submit feedback comments or guestbook entries containing spam messages. The text in the image is usually distorted to prevent the use of OCR (optical character reader) software to defeat the process. Hotmail, PayPal, Yahoo and a number of blog sites have employed this technique.
Purpose
What I wanted is to have a control which I could to put on my web page, and this control should immediatly prefent a form from spam.
The idea
I have written a webcontrol which is encapsulating
- an image : this image is using as source "CaptchaImage.axd"... this "page" is in fact a httphandler which will return an image in the response stream. this image is containing the "anti-spam" code and has been generated using the CaptchaImage object provided by BrainJar.
- a textbox : the user has to fill the image code in the textbox in order to ensure it is not an aumatised bot which is working on the form.
- a RequiredValidator : this will ensure that there is a data filled in the textbox
- a CustomValidator : it will test the text in the textbox with the anti-spam code which has been stored in session.
The Content

How to install it
- Add a reference to the CaptchaImage assembly.
- in the web.config file, you have two lines to add in the /configuration/system.web:
- in ./HttpHandler section , add the reference to the handler which returns a generated CaptchaImage
<httpHandlers>
<add verb="GET" path="CaptchaImage.axd" type="CaptchaImage.CaptchaImageHttpHandler"/>
</httpHandlers>
- in ./controls section, add the tag reference of the Captcha image assembly which contains the interesting webcontrol.
<controls>
<add tagPrefix="Captcha" namespace="CaptchaImage" assembly="CaptchaImage"/>
</controls>
- Well... almost is done... now, let's go to use it
How to use it
- Create your form to prevent from spams.
- Drag and drop the webcontrol which has appeared in your tool bar or add this tag
<Captcha:CaptchaControl runat="server" ID="captcha" />
- specify a ValidationGroup on your submit button and give the same name to the ValidationGroup property of the captcha control
ValidationGroup="Contact"
- An other property which you could get interest in is "MessageError"... this will set an error message that you could get through a validation summary like in the sample project.
MessageError="An error has occured at validation of anti-spam code.<br/> Pleade check it in again."
- The tag should have this appearance now:
<Captcha:CaptchaControl runat="server" ID="captcha" ValidationGroup="Contact"
MessageError="An error has occured at validation of anti-spam code.<br/> Pleade check it in again." />
- the last step to do is to add a test in the OnClick event of your submit button
protected void SubmitButton_Click(object sender, EventArgs e)
{
if (Page.IsValid) {
}
Requirements
learn about
- validators strategy provided by asp.net
- webcontrols
- HttpHandler
Conclusion
HttpHandlers are a quite new logic for me and I choose this technology mainly for the background works intead of building a web page. May be it's not done for this purpose... may be it is... but I think it's more logical to work like that when making some automatized generations...
CaptchaImage was a well built project and it was a pleasure to use it.
Greetz to
BrainJar. : http://www.codeproject.com/aspnet/CaptchaImage.asp
and every sources which are provided on the net
Sorry... as usual
for my poor english ;)