Simple ASP.NET CAPTCHA Tutorial

I will show you today how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages.

All you need to do is Create a ASPX page, that ASPX page will have a content type of image/jpeg, then within the code-behind file of that asp page we will be using system.drawing namespace to create a dynamic image.

System will generate 2 Random numbers, and then it will store the result of these 2 numbers into a Session Variable.

For Example

a = 2 + 3 (where 2 and 3 are 2 random numbers)

Session("Answer") = 5

then we will create a image using System.drawing namespace with the text " 2+ 5 = " within it.
We will store the result into a session variable, so later on our webpage we can use this session variable to compare with what user types.

Lets see the code of Captcha.aspx:

Imports System.Drawing

Partial Class Captcha
    Inherits System.Web.UI.Page

 

    Private Sub returnNumer()

        Dim num1 As New Random
        Dim num2 As New Random

        Dim numQ1 As Integer
        Dim numQ2 As Integer
        Dim QString As String


        numQ1 = num1.Next(10, 15)
        numQ2 = num1.Next(17, 31)

 

        QString = numQ1.ToString + " + " + numQ2.ToString + " = "
        Session("answer") = numQ1 + numQ2


        Dim bitmap As New Bitmap(85, 35)
        Dim Grfx As Graphics = Graphics.FromImage(bitmap)
        Dim font As New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Pixel)
        Dim Rect As New Rectangle(0, 0, 100, 50)

        Grfx.FillRectangle(Brushes.Brown, Rect)
        Grfx.DrawRectangle(Pens.PeachPuff, Rect) ' Border
        Grfx.DrawString(QString, font, Brushes.Azure, 0, 0)

 


        Response.ContentType = "Image/jpeg"
        bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)


        bitmap.Dispose()
        Grfx.Dispose()

 

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        Call Me.returnNumer()

    End Sub


End Class

 

So the Page_Load event Generates an Image.

 

now we can use this Page within any aspx page for verification. For Example here is form2.aspx

now you can create a TextBox, have users enter some text into it, and then create a ANSWER button, upon clicking of ANSWER button compare the value of textbox with the Session variable Session("answer"), if both are same then Verification passed.

if you have any questions, please reply to this post.

Thanks for reading

 - Saqib

 

Posted by:

Comments History


by: on
nat, Content type determines what kind(type) of documents needs to be generated. in this case its Image, it could be PDF,XLS etc hope that helps.
 
by: on
tks
 
by: on
Im new to web asp.net. Can someone post what the full ASPX page code would look like? Im using C#. Thanks
 
by: on
Thats awsome!
 
by: on
This method is not working when i deployed on IIS 5.0. Image not shown!! may u pls try to deploy it
 
by: on
edeeeeeee
 
by: on
im using c# visual studio 2008. I would like to implement this into it. So what code i need to change in order i can able to use captcha
 
by: on
hey man i want captcha work tell me how can i start it ?please provide me captcha software.and how can i get account and payment tell me ?
 
by: on
tyyy
 
by: on
dfgfdg
 
by: on
hello, your explanation is too good nd understandable. can u plz send me the zip file of this code. thanx.
 
by: on
I'm new to vb.net, but I like this simple code.  I'm am having a few issues implementing it.  Before posting here, I submitted the question here  http://stackoverflow.com/questions/12965010/vb-net-trying-simple-captcha

How is everyone else implementing this?  If I have it on a separate page, the session("answer") is one refresh behind.  I thought that as page one loads, and calls the page with this code, it would immediately update the session.  But it is not. 
 
by: on

Pride,
put the code that you have into a aspx.vb (code behind file) and within LOAD method of PAGE generate the image.

Then load the image on page whereever you want like this...

<img src="your_page_with_code.aspx" />

 

Name :  

Email :  

Comment Below