In this article I will explain with an example, how to implement Captcha control in ASP.Net using C# and VB.Net.
This article will explain, how to use MSCaptcha control in ASP.Net using C# and VB.Net.
 
 
Download MSCaptcha Library
You need to download the free MSCaptcha Library using the following link.
Note:The above DLL is also available in the Sample Code attached at the end of the Article.
 
 
MSCaptcha Implementation
Registering MSCaptcha Control
In order to use MSCaptcha control, you will need to add reference of MSCaptcha Library and then register on the Page as shown below.
<%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>
 
Adding MSCaptcha Control to the page
You need to add the following HTML Markup on the page where you want to implement Captcha control.
<cc1:CaptchaControl ID="captcha1" runat="server" CaptchaBackgroundNoise="Low" CaptchaLength="5"
    CaptchaHeight="60" CaptchaWidth="200" CaptchaLineNoise="None" CaptchaMinTimeout="5"
    CaptchaMaxTimeout="240" FontColor="#529E00" />
 
The Captcha control has been assigned with following properties:
CaptchaBackgroundNoise – It is used to set the amount of Noise you want in background noise.
CaptchaLength – It is used to specify the Length of the Captcha Text.
CaptchaHeight – It is used to specify the Height of Captcha control.
CaptchaWidth – It is used to specify Width of Captcha control.
CaptchaLineNoise – It is used to specify the Line Noise in image.
CaptchaMinTimeout – It is used to specify the Minimum Time Captcha image is valid.
CaptchaMaxTimeout – It is used to specify the Maximum Time Captcha image is valid.
 
Web.Config Modifications
You will need to modify the Web.Config file as shown below in Yellow in order to use the MSCaptcha control.
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.5"/>
        <httpHandlers>
            <add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>
        </httpHandlers>
        <authentication mode="Windows"/>
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
    </system.web>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <handlers>
            <add name="MSCaptcha.captchaImageHandler" verb="GET" path="CaptchaImage.axd" type="MSCaptcha.captchaImageHandler, MSCaptcha" resourceType="Unspecified"/>
        </handlers>
    </system.webServer>
</configuration>
 
 
HTML Markup
The following HTML markup consists of:
CaptchaControl – For displaying Captcha TextBox, an ASP.Net RequiredFieldValidator, a Label and a Button control.
RequiredFieldValidator – For displaying required validation message.
TextBox – For capturing Captcha text.
Label – For displaying success or error message.
Button – For validating Captcha value entered in the TextBox.
The Button has been assigned with an OnClick event handler.
 
Properties
The RequiredFieldValidator has been assigned with following property:
ControlToValidate – ID of the Control to be validated. Here it is ID of the Captcha control.
<%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>
 
<!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">
        <cc1:CaptchaControl ID="captcha1" runat="server" CaptchaBackgroundNoise="Low" CaptchaLength="5"
            CaptchaHeight="60" CaptchaWidth="200" CaptchaLineNoise="None" CaptchaMinTimeout="5"
            CaptchaMaxTimeout="240" FontColor="#529E00" />
        <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvCaptcha" runat="server" ErrorMessage="*Required"
            ControlToValidate="txtCaptcha" Display="Dynamic"></asp:RequiredFieldValidator>
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
        <br/><br/>
        <asp:Button ID="btnVerify" runat="server" Text="Verify" OnClick="OnVerify" />
    </form>
</body>
</html>
 
 
Namespases
You will need to import the following namespace.
C#
using System.Drawing;
 
VB.Net
Imports System.Drawing
 
 
Validating the Captcha
When Verify button is clicked, the TextBox value is fetched and trimmed hence it is Captcha value is validated using the ValidateCaptcha method of Captcha control.
Finally, using the UserValidated property of Captcha control, the Captcha is verified and appropriate message is displayed using the Label control.
C#
protected void OnVerify(object sender, EventArgs e)
{
    captcha1.ValidateCaptcha(txtCaptcha.Text.Trim());
    if (captcha1.UserValidated)
    {
        lblMessage.ForeColor = Color.Green;
        lblMessage.Text = "Valid";
    }
    else
    {
        lblMessage.ForeColor = Color.Red;
        lblMessage.Text = "InValid";
    }
}
 
VB.Net
Protected Sub OnVerify(sender As Object, e As System.EventArgs)
    captcha1.ValidateCaptcha(txtCaptcha.Text.Trim())
    If captcha1.UserValidated Then
        lblMessage.ForeColor = Color.Green
        lblMessage.Text = "Valid"
    Else
        lblMessage.ForeColor = Color.Red
        lblMessage.Text = "InValid"
    End If
End Sub
 
 
Screenshot
How to implement Captcha in ASP.Net
 
 
Demo
 
 
Downloads