Click here to Skip to main content
15,897,291 members
Articles / Web Development / ASP.NET

A CAPTCHA Server Control for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.84/5 (166 votes)
31 Jan 20077 min read 2M   32.5K   408  
A CAPTCHA control implemented as a simple, visual drag-and-drop Server Control for ASP.NET.
Imports System
Imports System.Web
Imports System.Drawing

''' <summary>
''' Captcha image stream HttpModule. Retrieves CAPTCHA objects from cache, renders them to memory, 
''' and streams them to the browser.
''' </summary>
''' <remarks>
''' You *MUST* enable this HttpHandler in your web.config, like so:
'''
'''	  &lt;httpHandlers&gt;
'''		  &lt;add verb="GET" path="CaptchaImage.aspx" type="WebControlCaptcha.CaptchaImageHandler, WebControlCaptcha" /&gt;
'''	  &lt;/httpHandlers&gt;
'''
''' Jeff Atwood
''' http://www.codinghorror.com/
'''</remarks>
Public Class CaptchaImageHandler
    Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
        Dim app As HttpApplication = context.ApplicationInstance

        '-- get the unique GUID of the captcha; this must be passed in via the querystring
        Dim guid As String = app.Request.QueryString("guid")
        Dim ci As CaptchaImage = Nothing

        If guid <> "" Then
            If String.IsNullOrEmpty(app.Request.QueryString("s")) Then
                ci = CType(HttpRuntime.Cache.Get(guid), CaptchaImage)
            Else
                ci = CType(HttpContext.Current.Session.Item(guid), CaptchaImage)
            End If

        End If

        If ci Is Nothing Then
            app.Response.StatusCode = 404
            context.ApplicationInstance.CompleteRequest()
            Return
        End If

        '-- write the image to the HTTP output stream as an array of bytes
        Dim b As Bitmap = ci.RenderImage
        b.Save(app.Context.Response.OutputStream, Drawing.Imaging.ImageFormat.Jpeg)
        b.Dispose()
        app.Response.ContentType = "image/jpeg"
        app.Response.StatusCode = 200
        context.ApplicationInstance.CompleteRequest()
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
My name is Jeff Atwood. I live in Berkeley, CA with my wife, two cats, and far more computers than I care to mention. My first computer was the Texas Instruments TI-99/4a. I've been a Microsoft Windows developer since 1992; primarily in VB. I am particularly interested in best practices and human factors in software development, as represented in my recommended developer reading list. I also have a coding and human factors related blog at www.codinghorror.com.

Comments and Discussions