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:
Align – It is used to align the Captcha image along with text or other controls.
Color – It is used to set the color of the Captcha Text. Default value is black.
 

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.8"/>
        <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:
ASPNET_Captcha – For generating Captcha.
TextBox – For entering the value.
RequiredFieldValidator – For validating TextBox.
The RequiredFieldValidator has been assigned with following property:
ControlToValidate – For defining the control to be validated. Here it is TextBox.
Button – For validating the TextBox value.
The Button has been assigned with an OnClick event handler.
<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" />
 
 

Namespaces

You will need to import the following namespace.
C#
using System.Drawing;
 
VB.Net
Imports System.Drawing
 
 

Validating the Captcha

When the Submit Button is clicked, the TextBox value is validated using the Validate method of Captcha control which returns Boolean value.
If the TextBox value matched with the Captcha value, it will return TRUE else FALSE.
Then, a check is performed based on the returned value and an 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

Mathematical Captcha  in ASP.Net using C# and VB.Net
 
 

Demo

 
 

Downloads