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.
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:
CaptchBackgroundNoise – 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 following shown 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 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 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#
VB.Net
Validating the Captcha
Following event handler is executed, when the Verify Button is clicked.
Inside this event handler, the TextBox value is validated using the ValidateCaptcha method.
Finally, using the UserValidated property Captcha is verified and appropriate message is displayed in 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
Demo
Downloads