In this article I will explain with an example, how to implement Captcha in ASP.Net using C# and VB.Net.
Download ASPSnippets Captcha DLL
You can download the latest ASPSnippets.Captcha.dll from the following link.
Note: You will need to add the reference of ASPSnippets.Captcha DLL in your project.
HTML Markup
The HTML markup consists of following controls:
TextBox – For capturing PersonId, Name and City.
DropDownList – For capturing Gender.
Captcha implementation
Following controls are required for Captcha implementation.
TextBox – For capturing Captcha answer.
Image – For displaying Captcha image.
CustomValidator – For validating Captcha.
Properties
ErrorMessage – For displaying error message when validation fails.
Events
The CustomValidator has been assigned with an OnServerValidate event handler for performing Captcha validation.
Button – For submitting the Form.
<table cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Person Details</th>
</tr>
<tr>
<td>Person Id: </td>
<td><asp:TextBox ID="txtPersonId" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Name: </td>
<td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Gender: </td>
<td>
<asp:DropDownList ID="ddlGenders" runat="server" Width="170">
<asp:ListItem Text="Please Select" Value=""></asp:ListItem>
<asp:ListItem Text="Male" Value="M"></asp:ListItem>
<asp:ListItem Text="Female" Value="F"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>City: </td>
<td><asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Image ID="imgCaptcha" runat="server"/></td>
<td>
<asp:TextBox ID="txtCaptchaAnswer" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvCaptchaValidate" runat="server" OnServerValidate="OnCaptchaValidate"
ErrorMessage="Captcha invalid" ForeColor="Red"></asp:CustomValidator>
</td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" /></td>
</tr>
</table>
Adding reference of ASPSnippets.Captcha DLL in project
1. Inside the Solution Explorer, right click on Project and then click on Add and then click on Reference.
2. Then, inside the Reference Manager window, click on Browse button.
3. Locate the ASPSnippets.Captcha and select it.
4. The ASPSnippets.Captcha DLL is now visible in the Reference Manager window. Select it and click OK button as shown below.
Finally, the ASPSnippets.Captcha DLL is now referenced.
Namespaces
You will need to import the following namespace.
C#
using ASPSnippets.Captcha;
VB.Net
Imports ASPSnippets.Captcha
Implementing Arithmetic Captcha in ASP.Net
In the code-behind, a property of class Captcha is created.
Page_Load
Inside the Page_Load event handler, Captcha object is initialized and its width, height, Font size, Font color, background color and Mode are set.
Note: The ASPSnippets.Captcha works in four different Modes i.e. Numeric, Alphabets, AlphaNumeric and Arithmetic. This article will use AlphaNumeric mode.
Finally, the Image control is set with the ImageData property.
C#
private Captcha Captcha
{
get
{
return (Captcha)ViewState["Captcha"];
}
set
{
ViewState["Captcha"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Captcha = new Captcha(125, 40, 20f, "#FFFFFF", "#61028D", Mode.AlphaNumeric);
imgCaptcha.ImageUrl = this.Captcha.ImageData;
}
}
VB.Net
Private Property Captcha As Captcha
Get
Return CType(ViewState("Captcha"), Captcha)
End Get
Set(ByVal value As Captcha)
ViewState("Captcha") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.Captcha = New Captcha(125, 40, 20.0F, "#FFFFFF", "#61028D", Mode.AlphaNumeric)
imgCaptcha.ImageUrl = Me.Captcha.ImageData
End If
End Sub
Validating Captcha using C# and VB.Net
When the Submit button is clicked, the OnCaptchaValidate event handler of the CustomValidator is executed.
Inside this event handler, the Captcha is validated using the IsValid method to which the Answer inputted by the User is passed.
If the IsValid method returns FALSE, the Error Message is displayed using the CustomValidator.
C#
protected void OnCaptchaValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = this.Captcha.IsValid(txtCaptchaAnswer.Text.Trim());
if (e.IsValid)
{
//Perform operations here.
int personId = int.Parse(txtPersonId.Text.Trim());
string name = txtName.Text.Trim();
string gender = ddlGenders.SelectedItem.Value;
string city = txtCity.Text.Trim();
}
}
VB.Net
Protected Sub OnCaptchaValidate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
e.IsValid = Me.Captcha.IsValid(txtCaptchaAnswer.Text.Trim())
If e.IsValid Then
'Perform operations here.
Dim personId As Integer = Integer.Parse(txtPersonId.Text.Trim())
Dim name As String = txtName.Text.Trim()
Dim gender As String = ddlGenders.SelectedItem.Value
Dim city As String = txtCity.Text.Trim()
End If
End Sub
Screenshot
Demo
Downloads