In this article I will explain how to use the free simple Captcha control called as ASPNet_Captcha in ASP.Net which allows you to make the Captcha validation simpler by providing mathematical challenges to user.

STEP 1
Firstly you need to add the reference of the ASPNet_Captcha to your ASP.Net Website as shown in the figure below.
Free Mathematical Captcha in ASP.Net
 
STEP 2
Now you need to add the following to the <httpHandlers> section of <system.web> or <handlers>section of <system.webServer> for IIS7 in the web.config  as shown below
 
IIS 6
 
<httpHandlers>
    <add verb="GET" path="Image.ashx" type="ASPNET_Captcha.Image, ASPNET_Captcha" />
</httpHandlers>
 
IIS 7
<handlers>
     <add name ="Captcha" verb="*" path="Image.ashx" type="ASPNET_Captcha.Image, ASPNET_Captcha" resourceType="Unspecified"/>
</handlers>
 
STEP 3
Now you simply need to add the ASPNET_Captcha Control to your ASP.Net Web page by registering it in the page header section
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register TagPrefix="uc" Namespace="ASPNET_Captcha" Assembly="ASPNET_Captcha" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <uc:ASPNET_Captcha ID="ucCaptcha" runat="server" Align = "Middle" Color = "#FF0000" />
    <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" />
    <br />
    <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>
 
That’s it you need to make it work.
 
Validating the Captcha
To validate the Captcha Control you need to do the following
C#
protected void Submit(object sender, EventArgs e)
{
    bool isValid = ucCaptcha.Validate(txtCaptcha.Text.Trim());
    if (isValid)
    {
        lblMessage.Text = "Valid!";
        lblMessage.ForeColor = Color.Green;
    }
    else
    {
        lblMessage.Text = "Invalid!";
        lblMessage.ForeColor = Color.Red;
    }
}
 
VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As System.EventArgs)
 Dim isValid As Boolean = ucCaptcha.Validate(txtCaptcha.Text.Trim)
 If isValid Then
       lblMessage.Text = "Valid!"
       lblMessage.ForeColor = Color.Green
 Else
       lblMessage.Text = "Invalid!"
       lblMessage.ForeColor = Color.Red
 End If
End Sub


The below screenshot describes the working the ASPNet_Captcha control
Free Mathematical Captcha in ASP.Net 3.5 4.0 2.0


Captcha Properties
In the first version of this free Captcha Control there are two properties
1. Color – This property allows you to define the color of the Captcha Text. Default is black.
2. Align – This property is useful for aligning the Captcha image along with text or other controls.
 
Advantages
The advantage of this Captcha is that it does not make use of Session or ViewState and can be easily used with jQuery which I’ll explain in my next article.
 
Downloads
You can download the complete sample source code in VB.Net and C# using the download link provided below.
ASPNet_Captcha_Demo.zip