<

Simple ASP.NET CAPTCHA

Published on
8,185 Points
2,185 Views
Last Modified:
Approved
Saqib Khan
Software Developer for Web & Desktop Applications
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.

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

Open in new window


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.

Any questions please post below.

Thanks for reading.
0
Comment
Author:Saqib Khan
3 Comments
 
LVL 8

Expert Comment

by:Camillia
Why not use Google's captcha? I recently implemented in my asp.net and it works great.
0
 
LVL 23

Author Comment

by:Saqib Khan
Absolutely, the purpose of this post was to show how to create a captcha yourself.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
There is a reason why all of the CAPTCHAs that you see these days are very squiggly, borderline unreadable texts:  OCR is very easy to perform against normal text. The kind of CAPTCHA presented here is good for a personal or family blog, at best. For something with a larger audience or having a higher risk/value, a more established CAPTCHA should be employed.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month