Click here to Skip to main content
15,396,498 members
Articles / Web Development / ASP.NET
Posted 7 Nov 2002


144 bookmarked

ASP.NET Controls to prevent automatic registrations from bots

Rate me:
Please Sign up or sign in to vote.
3.80/5 (29 votes)
7 Nov 20026 min read
A set of controls that when placed on a web form help to prevent abuse of registration pages by forcing users to enter the security code displayed in a picture.


AntiAuto is a set of controls that can be used on an ASP.NET Web Form to prevent automatic registrations from 'bots. It was something mentioned in the Lounge fairly recently and I thought it would be a good example for ASP.NET's superior application architecture.

Many sites require users to register before they're given full access. For example, requires you to enter a few basic details to provide you with an email address. However, this type of service is open to abuse - the automatic generation of many email addresses to be used for sending spam by a piece of software that automatically completes the registration process generating random user names etc.

To prevent this type of abuse many sites require users to enter a code that is displayed in a picture -- thus forcing the person to enter the registration details.

The aim is to produce a set of ASP.NET control's that can be easily deployed, and will fit in around an existing registration procedure.

Solution Overview

To give an overview to the classes within the project, here's a quick UML class diagram.

The important classes being CodeImage and CodeImageValidator. Util is a supportive class, and provides the encryption and decryption functionality needed by various other parts.


The purpose of this class is to produce the HTML on the web form that will display the image. The image itself is generated by the DrawImage.aspx file, which in turn uses the PictureGenerator class.


This is a BaseValidator derived class that verifies that the contents of the associated text box (ControlToValidate) matches what has been drawn within the CodeImage control (CodeImageControl).

How they work together

When a page is first loaded a random number is generated, this is then passed as part of a query string to the DrawImage.aspx page. This number is retained in ViewState throughout any postbacks that occur. During a postback the validation control (CodeImageValidator) is used to ensure that the form can be validated successfully.

Implementation Details


CodeImage is implemented in the CodeImage.cs file. The purpose of the control is to output the necessary HTML code to draw the image. For example, if the DrawImage.aspx page were to draw the picture the HTML output would look like:

<img src="DrawImage.aspx?code=12345">

The code for the entire class is as follows:

public class CodeImage : Control
    private string _key;
    private int _digits;
    public CodeImage()
        _key = System.Configuration.ConfigurationSettings.AppSettings["EncryptionKey"];
        _digits = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["Digits"]);

    public string Code
            return (string)ViewState["SecretCode"];
            ViewState["SecretCode"] = value;

    protected override void Render(HtmlTextWriter output)
        output.Write( String.Format("<img src=\"DrawImage.aspx?code={0}\">", 

    protected override void OnLoad(System.EventArgs e)
        if (!this.Page.IsPostBack)
            StringBuilder sbCode = new StringBuilder(_digits,_digits);

            Random R = new Random();
            int i;
            int MaxLimit = 9;
            for(i = 0; i < _digits; i++)
            Code = Util.EncryptString(sbCode.ToString(),_key);

It's extremely simple code, loading a couple of configuration settings in the Constructor. These are used across the various controls and pages, so using the web.config configuration file is the best way of doing it.

The Render method is overridden to write out the HTML code, the security code to be displayed in the picture comes from the Code property. Because the HTML code will be visible to users its necessary to encrypt the number before putting it into the QueryString, so instead of using code=12345 it would be code=jA89AlxmmA etc. This encryption is performed by the EncryptString method within the Util class. The code is then decrypted by the PictureGenerator class when its asked to render the image.

It's also important to note that the encrypted security code is UrlEncode'd first, ensuring that any invalid characters can be made safe before being placed into the QueryString. Again, these are then UrlDecode'd the other side (within DrawImage.aspx).

The OnLoad event is used to generate a random number if the page is being loaded for the first time (i.e. not within a Post Back). Once a security code has been generated it is stored in the Code property, which in turn stores it in the ViewState (thus allowing the contents to be persisted across postbacks).

The CodeImage control is to be put directly into an ASP.NET Web Form, and the code to do that is included later in the article.


This class is implemented in the CodeImageValidator.cs file, and is used to check that the contents of the associated text box match the code generated by the CodeImage control.

Again, the code is relatively simple so here's the entire class implementation before the discussion:

public class CodeImageValidator : BaseValidator
    TextBox _codeTextBox;
    string _codeImageId;
    CodeImage _codeImageControl;
    string _key;

    public CodeImageValidator()
        _key = System.Configuration.ConfigurationSettings.AppSettings["EncryptionKey"];

    protected override bool ControlPropertiesValid()
        // Should have a text box control to check
        Control ctrl = FindControl(ControlToValidate);
        Control imageControl = FindControl(_codeImageId);

        if ( (null != ctrl) && (null != imageControl) )
            if ((ctrl is System.Web.UI.WebControls.TextBox) &&
                (imageControl is CodeImage))
                _codeTextBox = (System.Web.UI.WebControls.TextBox) ctrl;
                _codeImageControl = (CodeImage) imageControl;

                return ( (null != _codeTextBox) && (null != _codeImageControl) );
                return false;
            return false;

    public string CodeImageControl
            return _codeImageId;
            _codeImageId = value;

    protected override bool EvaluateIsValid()
        return (Util.DecryptString(_codeImageControl.Code,_key) == 

CodeImageValidator is derived from BaseValidator and so is expected to behave as any other Validation Control would. It needs to override the EvaluateIsValid method that will be called when the form is submitted.

The ControlPropertiesValid method is used to obtain references to the controls that are used during validation -- the TextBox and the CodeImage controls. These references are then maintained in the ctrl and imageControl fields. Two properties of the CodeImageValidator control are used to set these in the ASPX page, ControlToValidate and CodeImageControl.

The CodeImage Control has a property (Code) that contains the encrypted security code that is to be entered into the text box. This is then decrypted in the EvaluateIsValid method and compared to the text box's contents. If they match then the validation will have succeeded and the method can return true.


The DrawImage page is used to produce the JPEG stream to send to the browser and uses the PictureGenerator class. The code to the DrawImage page is as follows:

<%@ Page Language="C#" ContentType="image/jpeg" %>
<script language="C#" runat="server">
    void Page_Load (Object sender, EventArgs e)
        // Draw the output
        Etier.AntiAuto.PictureGenerator.OutputPicture( this, 
                HttpUtility.UrlDecode(Request.QueryString["code"]) );

First the ContentType is set to image/jpeg (ensuring that the browser renders the contents as an image, as opposed to an HTML page or any other type). The OutputPicture method is called, passing a reference to the current page and the encrypted security code to be rendered.

The code to the OutputPicture method is as follows:

public static void OutputPicture(System.Web.UI.Page page, string encryptedCode)
    string key = System.Configuration.ConfigurationSettings.AppSettings["EncryptionKey"];
    int digits = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["Digits"]);

    Bitmap codeBitmap = new Bitmap((digits*16)+20,26, PixelFormat.Format24bppRgb);
    Graphics g = Graphics.FromImage(codeBitmap);    

    g.SmoothingMode = SmoothingMode.AntiAlias;

    g.DrawString( Util.DecryptString(encryptedCode,key), new Font("Arial", 16,
                  FontStyle.Bold ), SystemBrushes.WindowText, new Point(10,2));

    codeBitmap.Save( page.Response.OutputStream, ImageFormat.Jpeg);

The code is based on Nick Parker's "Web Graphics On The Fly in ASP.NET" article here at CodeProject. It loads the configuration settings and then renders the image, decrypting the security code first. The security code is taken from DrawImage.aspx's QueryString and was encrypted by the CodeImage control to prevent visitors determining the security code outside of the rendered picture.

How to use it

The demo application includes a ready-to-deploy sample, but there are a couple of things that would have to be done to an existing ASP.NET Web Form:

  1. Copy the AntiAuto assembly to the application's /bin directory
    Copy the web.config configuration file to the application's root
    Copy DrawImage.aspx to the same directory containing the Web Form
  2. Add an import command to the top of the web form:
    <%@ Register TagPrefix="etier" Namespace="Etier.AntiAuto" Assembly="AntiAuto" %>
    This maps a namespace to a tag prefix, such that you can then add controls in the form of <prefix:ControlClass.
  3. Add a CodeImage control to the web form:
    At run time this produces the <img src="..."> HTML code to produce the image.
  4. Add a CodeImageValidator control to the web form:
        ErrorMessage="Please enter the text in the image"
    It's important that the ControlToValidate and CodeImageControl are set to the Id's you gave to the TextBox and CodeImage Controls.


The controls are very simple and demonstrate how glorious ASP.NET's new architecture is. I hope people find it useful, and improve it. Apologies for not including many (if any) comments within the code, but since this article covers how it all works together it should be relatively to understand. As I revisit the solution I'll neaten things up a bit, one thing I would definitely like to do is produce a single control so that deployment is made even easier.

Please feel free to post a message below or email me if you have any questions or comments.


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


About the Author

Paul Ingles
Web Developer
United Kingdom United Kingdom
I graduated with a first class BSc honours degree in a Computer Science/Management Science hybrid Smile | :) from Loughborough University. I live in London and currently work as a .NET Developer in central London.

I'm also currently blogging my progress at further developing my articles into commercial components. Read my blog here.

I've also recently started dabbling at digital photography, and digital re-touching, and developing small simple multiplayer games in Flash.

Comments and Discussions

Generalsome corrections Pin
Tomas Kubes15-Dec-08 23:21
MemberTomas Kubes15-Dec-08 23:21 
GeneralThe image doesn't appear sometime Pin
Steph20047-Jan-06 7:48
MemberSteph20047-Jan-06 7:48 
GeneralRe: The image doesn't appear sometime Pin
Bollis19-Nov-06 12:52
MemberBollis19-Nov-06 12:52 
GeneralRe: The image doesn't appear sometime Pin
jacquesr15-Apr-07 16:49
Memberjacquesr15-Apr-07 16:49 
AnswerRe: The image doesn't appear sometime Pin
ktoc111-Jul-07 5:48
Memberktoc111-Jul-07 5:48 
Questionerror creating control Pin
kencodeproj31-Dec-05 2:08
Memberkencodeproj31-Dec-05 2:08 
AnswerRe: error creating control Pin
xiezhenhua16-Jun-06 23:00
Memberxiezhenhua16-Jun-06 23:00 
AnswerRe: error creating control Pin
xiezhenhua16-Jun-06 23:01
Memberxiezhenhua16-Jun-06 23:01 
GeneralBUG HttpUtility.UrlDecode(Request.QueryString[&quot;code&quot;]) Pin
Member 183834814-Oct-05 10:24
MemberMember 183834814-Oct-05 10:24 
AnswerRe: BUG HttpUtility.UrlDecode(Request.QueryString[&quot;code&quot;]) Pin
Bryan Kadzban5-May-06 5:58
MemberBryan Kadzban5-May-06 5:58 
Generalexception while encoding text and random lines on picture Pin
Cristian Romanescu10-Jun-05 6:57
MemberCristian Romanescu10-Jun-05 6:57 
GeneralRe: exception while encoding text and random lines on picture Pin
Cristian Romanescu10-Jun-05 7:04
MemberCristian Romanescu10-Jun-05 7:04 w/ Pin
Metal2000man5-May-05 5:18
MemberMetal2000man5-May-05 5:18 
Generalvisions ebony Pin
Anonymous7-Mar-05 13:31
MemberAnonymous7-Mar-05 13:31 
GeneralThanks for the article Pin
Member 136829031-Jan-05 18:03
MemberMember 136829031-Jan-05 18:03 
GeneralSession time-out problems, I think... Pin
Danny Alvares19-Apr-04 12:56
MemberDanny Alvares19-Apr-04 12:56 
GeneralRe: Session time-out problems, I think... Pin
lnarvaez16-Jun-04 13:57
Memberlnarvaez16-Jun-04 13:57 
GeneralRe: Session time-out problems, I think... Pin
Danny Alvares18-Jun-04 1:32
MemberDanny Alvares18-Jun-04 1:32 
GeneralUnavailability of demo Pin
Paul Ingles8-Feb-04 12:03
MemberPaul Ingles8-Feb-04 12:03 
GeneralValidation doesn't work Pin
Danny Alvares24-Dec-03 11:25
MemberDanny Alvares24-Dec-03 11:25 
GeneralRe: Validation doesn't work Pin
TC602256-Jan-04 4:03
MemberTC602256-Jan-04 4:03 
GeneralRe: Validation doesn't work Pin
Danny Alvares8-Jan-04 10:51
MemberDanny Alvares8-Jan-04 10:51 
GeneralOutstanding Pin
Silmar0130-Jul-03 12:29
MemberSilmar0130-Jul-03 12:29 
GeneralAnti Auto Problem Pin 12:02 12:02 
GeneralRe: Anti Auto Problem Pin
Christian Abeln2-Dec-03 22:51
MemberChristian Abeln2-Dec-03 22:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.