In this article I will explain with an example, how to use Mathematical Captcha control in ASP.Net using C# and VB.Net.
This article will explain, how to use ASPNET_Captcha in ASP.Net using C# and VB.Net.
 
 
Download ASPNET_Captcha Library
You need to download the free ASPNET_Captcha Library using the following link.
 
 
ASPNET_Captcha Implementation
Registering ASPNET_Captcha Control
In order to use ASPNET_Captcha control, you will need to add reference of ASPNET_Captcha Library and then register on the Page as shown below.
<%@Register TagPrefix="uc" Namespace="ASPNET_Captcha" Assembly="ASPNET_Captcha" %>
 
Adding ASPNET_Captcha Control to the page
You need to add the following HTML Markup on the page where you want to implement ASPNET_Captcha control.
<uc:ASPNET_Captcha ID="captcha1" runat="server" Align="Middle" Color="#FF0000" />
 
The ASPNET_Captcha control has been assigned with following properties:
Color – It is used to set the color of the Captcha Text. Default value is black.
Align – It is used to align the Captcha image along with text or other controls.
 
Web.Config Modifications
You will need to modify the Web.Config file as following shown Yellow in order to use the ASPNET_Captcha control.
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.5"/>
        <httpHandlers>
            <add verb="GET" path="Image.ashx" type="ASPNET_Captcha.Image, ASPNET_Captcha"/>
        </httpHandlers>
    <pages controlRenderingCompatibilityVersion="4.0"/>
    </system.web>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <handlers>
            <add name ="Captcha" verb="*" path="Image.ashx" type="ASPNET_Captcha.Image, ASPNET_Captcha" resourceType="Unspecified"/>
        </handlers>
    </system.webServer>
</configuration>
 
 
HTML Markup
The following HTML markup consists of Captcha control, a TextBox, an ASP.Net RequiredFieldValidator, a Label and a Button control.
The Button has been assigned with an OnClick event handler.
The RequiredFieldValidator has been assigned with following property:
ControlToValidate – ID of the Control to be validated.
<%@Register TagPrefix="uc" Namespace="ASPNET_Captcha" Assembly="ASPNET_Captcha" %>
 
<!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <uc:ASPNET_Captcha ID="captcha1" runat="server" Align="Middle" Color="#FF0000" />
        <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvCaptcha" runat="server" ErrorMessage="*Required"
            ControlToValidate="txtCaptcha" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator>
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
        <br/>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" />
    </form>
</body>
</html>
 
 
Namespases
You will need to import the following namespace.
C#
using System.Drawing;
 
VB.Net
Imports System.Drawing
 
 
Validating the Captcha
Following event handler is executed, when the Submit Button is clicked.
Inside this event handler, the TextBox value is validated using the Validate method and returns True or False.
Finally, based on the returned value, appropriate message is displayed in the Label control.
C#
protected void Submit(object sender, EventArgs e)
{
    bool isValid = captcha1.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(sender As Object, e As System.EventArgs)
    Dim isValid As Boolean = captcha1.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
 
 
Screenshot
Free ASP.Net Mathematical Captcha Control in VB.Net and C#
 
 
Demo
 
 
Downloads