How to implement simple Captcha in ASP.Net.


Introduction

In this post, I am explain How to implement simple Captcha in ASP.Net.

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a reference of "SRVTextToImage.dll".

Download SRVTextToImage.dll
Go to Solution Explorer > Right Click on References > Add Reference... > Browse > select "SRVTextToImage.dll" > OK.

Step-3: Create a new Page & design for implement Captcha.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

Here I have created "Feedback.aspx"
HTML Code
         <table>
            <tr>
                <td>Email ID : </td>
                <td><asp:TextBox ID="txtEmailID" runat="server" Width="300px"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Your Feedback :</td>
                <td>
                    <asp:TextBox ID="txtFeedback" runat="server" TextMode="MultiLine" Width="300px" Height="50px" />
                </td>
            </tr>
            <tr>
                <td>
                    Security Code :
                </td>
                <td>
                    <asp:Image ID="imgCaptcha" runat="server" ImageUrl="~/CaptchaImage.aspx" />
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:TextBox ID="txtCaptchaText" runat="server" Width="100px" /> [Type Security code here]
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server"  Text="Submit" OnClick="btnSubmit_Click"/>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <asp:Label ID="lblMessage" runat="server" />
                </td>
            </tr>
        </table>
        
HTML Code

Step-4: Create a new Page for Generate Captcha Image.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form under Web > Enter page name > Add.

Here I have created "CaptchaImage.aspx"

            <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CaptchaImage.aspx.cs" Inherits="ASPCreateCaptcha.CaptchaImage" %>
        

[N:B: Delete all the html code from the page & Don't use master page ]

Step-5: write below code in page_load event of "CaptchaImage.aspx" for Generate Captcha Image.

Import the followings...

             using SRVTextToImage;
            using System.Drawing;
            using System.Drawing.Imaging;
        

            protected void Page_Load(object sender, EventArgs e)
            {
                CaptchaRandomImage CI = new CaptchaRandomImage();
                // GetRandomString Function return random text of your provided characters size
                string captchaText = CI.GetRandomString(5);

                //GenearteImage  function return image of the provided text of provided size
                //CI.GenerateImage(captchaText, 200, 50);
                //there is a overload function available for set color of the image
                Session["CaptchaText"] = captchaText;
                CI.GenerateImage(captchaText, 200, 50, Color.DarkGray, Color.White);

                this.Response.Clear();
                this.Response.ContentType = "image/jpeg";
                CI.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);
                CI.Dispose();
            }
        

Step-6: Write below code in the button click event (in this page here Feedback.aspx) for validate captcha

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.


            protected void btnSubmit_Click(object sender, EventArgs e)
            {
                // Other Validation Here

                // Here I will validate Captcha 
                bool isCaptchaValid = false;
                if (Session["CaptchaText"] != null && Session["CaptchaText"].ToString() == txtCaptchaText.Text)
                {
                    isCaptchaValid = true;
                }

                if (isCaptchaValid)
                {
                    lblMessage.Text = "Captcha Validation Success";
                    lblMessage.ForeColor = Color.Green;
                }
                else
                {
                    lblMessage.Text = "Captcha Validation Failed";
                    lblMessage.ForeColor = Color.Red;
                }

                // Write Remaining code here for perform insert / update etc...
            }
        

Step-7: Run Application.


Download Application     Live Demo




Related Post : 


Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.