KnowlegeZone.com, Share knowledge Gain knowledge
New Articles:
     

Home > ASP : Simple ASP.NET CAPTCHA Tutorial

Article By: Saqib   Date: 4/6/2009

Simple ASP.NET CAPTCHA Tutorial

I will show you 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

<asp:image runat=server imgeurl="captcha.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

 - Saqib

 


Want to buy or sell your ASP books? use the link below!
Click Here To Sell Yours (New/Used) | Click Here To Buy New/Used! | Advertise here

Share |


Comments.
Comment/Solution Posted By: Jason    Date: 4/6/2009 3:14:00 PM
thanks

Comment/Solution Posted By: Eric    Date: 5/14/2009 8:32:00 AM
Thank you :]

Comment/Solution Posted By: ashkan    Date: 6/5/2009 11:39:00 AM
very nice tutorial thank you

Comment/Solution Posted By: simp    Date: 6/10/2009 6:36:00 PM
Thanks, not bad.

Comment/Solution Posted By: ilya    Date: 6/25/2009 1:19:00 PM
Great tutorial,but is it safe to store the answer in session?

Comment/Solution Posted By: Saqib    Date: 6/26/2009 1:49:00 AM
You are simply storing the answer for current user. it cant not be hijacked so easily.

if you are really concerned then have your Form hosted over HTTPS channel.

Comment/Solution Posted By: websmartindia.com    Date: 7/20/2009 12:43:00 PM
good work

Comment/Solution Posted By: Jeevesh    Date: 8/28/2009 6:14:00 AM
Cool stuff, thanx

Comment/Solution Posted By: biduth    Date: 9/23/2009 5:02:00 AM
Fine

Comment/Solution Posted By: samson daniel    Date: 1/15/2010 6:00:00 AM
Iam dont know the asp language can you please send me the whole codes in the pages where it works please to my email.
thanks
daniel
could be very helpfull

Comment/Solution Posted By: samson daniel    Date: 1/19/2010 4:38:00 AM
My dear friend I dont know asp please can you help me to download the whole page in a readymade winrar or winzip file and then i can read all the pages please let me know the url where i could get the complete pages.
thanks,
daniel

Comment/Solution Posted By: skhan    Date: 1/19/2010 1:07:00 PM
Samson, above the complete page code, just save it into a Vb.net Class.

Comment/Solution Posted By: bmw    Date: 1/20/2010 10:42:00 PM
hi,
i have created two files. but i am not getting image displayed. can you send me files, please?

thx

Comment/Solution Posted By: skhan    Date: 1/21/2010 12:54:00 AM
@bmw,send me your code at saqi19(AT)hotmail.com, i will see whats wrong with your code.

Comment/Solution Posted By: swalens    Date: 2/4/2010 4:34:00 AM
works fine

Comment/Solution Posted By: annop    Date: 2/12/2010 12:12:00 AM
Great job!

Comment/Solution Posted By: maul    Date: 4/4/2010 3:45:00 AM
how can I get the "Session variable Session("answer")" in the different page

Comment/Solution Posted By: captcha    Date: 4/4/2010 3:04:00 PM
Sessions are available in all the pages. this is the beauty of sessions.

Comment/Solution Posted By: samson daniel    Date: 4/5/2010 7:01:00 AM
will to let me know from where your codes starts and where it ends. I just a website designer and dont have any knowledge of codes. please help

Comment/Solution Posted By: Sears Parts    Date: 4/14/2010 4:17:00 PM
Nice post & nice blog. I love both.

a href="http://www.searspartssite.com"Sears Parts/a



Pages :2 
Post Comment or Solution   OR   Sign in using Google/Aol/Yahoo/Msn/OpenID
Your Name:  
Your Email:  
Comments:
 
Please Verify:
(Type Answer)