How to implement Captcha in Captcha. In this post I will create simple CAPTCHA image using ASP.Net with and CAPTCHA is used to identify end user as a human.
Idea is, system will store randomly generated text into session. later the text would be rendered as an image (Convert text to image.)

Step 1: generic handler:

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging; 
public class Handler : IHttpHandler,IRequiresSessionState 
    public void ProcessRequest(HttpContext context)
        using (Bitmap b = new Bitmap(250, 50))
            Font f = new Font("Arial", 10F);
            Graphics g = Graphics.FromImage(b);
            SolidBrush whiteBrush = new SolidBrush(Color.Blue);
            SolidBrush blackBrush = new SolidBrush(Color.White);
            RectangleF canvas = new RectangleF(0, 0, 250, 50);
            g.FillRectangle(whiteBrush, canvas);            
            context.Session["Captcha"] = GetRandomString();
            g.DrawString(context.Session["Captcha"].ToString(), f, blackBrush, canvas);
            context.Response.ContentType = "image/gif";
            b.Save(context.Response.OutputStream, ImageFormat.Gif);
    public bool IsReusable
            return false;
    private string GetRandomString()
        string []arrStr = "A,B,C,D,1,2,3,4,5,6,7,8,9,0".Split(",".ToCharArray());
        string strDraw = string.Empty;
        Random r = new Random();         
         for(int i = 0; i < 5 ; i++)
              strDraw += arrStr[r.Next(0,arrStr.Length-1)];
        return strDraw;

how add generic handeler:

Step 2: Code behind page code :
You just Create Default.aspx page and copy the below code from authore code to .aspx page.

<img src="Handler.ashx" /> 
<br /> 
Please type above text here. To identify your self as a human 
<br /> 
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> 
<br /> 
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Step 3: button click Event Code:
On .cs page paste this code for button click.

 protected void Button1_Click(object sender, EventArgs e)
        if (Session["Captcha"].ToString() == TextBox1.Text)
            Label1.Text = " System identified you as a human";
            Label1.Text = "Please try again";
            TextBox1.Text = "";

Author: Pavan

I am developer have good knowledge of ver 05,08,10 and good hand in sql server.Proficient in Object Oriented Programming and javascript, jQuery. Achievements - Integrate Spotfire, appnexus API with sql server. Hobbies - Blogging ,Games, Movies ,Teaching,Keeping myself update with new technologies

2 thoughts on “How to implement Captcha in”

  1. i tried this in vs2010…but it will give error in handler.ashx as below…
    Error3The page must have a <%@ webhandler class=”MyNamespace.MyClass” … %> directive.C:\Users\DvD\Documents\Visual Studio 2010\WebSites\capcha\Handler.ashx1

  2. Hello  satish ,
                      Please make sure your Handler page directive is in correct format or not?When you create handler then it will show you directive at the top of page please verify with the name:

    <%@ WebHandler Language=”C#” Class=”Handler” %>
      let me know if you face any problem. I used same code for VS 2010 web application  it working fine.

Comments are closed.