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

A CAPTCHA Control for ASP.NET 2

Rate me:
Please Sign up or sign in to vote.
4.86/5 (55 votes)
14 Mar 2008LGPL35 min read 359.3K   5.6K   174  
A CAPTCHA control that is simple, secure, and easy to use.
using System;
using SecureCaptcha.Security.Cryptography;

public delegate void CaptchaEventHandler();

public partial class CaptchaControl : System.Web.UI.UserControl
{
    private string color = "#ffffff";
    protected string style;
    private event CaptchaEventHandler success;
    private event CaptchaEventHandler failure;
    
    public string Message
    {
        // We don't set message in page_load, because it prevents us from changing message in failure event
        set { lblCMessage.Text = value; }
        get { return lblCMessage.Text;  }
    }
    public string BackgroundColor
    {
        set { color = value.Trim("#".ToCharArray()); }
        get { return color; }
    }
    public string Style
    {
        set { style = value; }
        get { return style; }
    }
    public event CaptchaEventHandler Success
    {
        add { success += value; }
        remove { success += null; }
    }
    public event CaptchaEventHandler Failure
    {
        add { failure += value; }
        remove { failure += null; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetCaptcha();
            txtCaptcha.Text = "";
        }
    }
    private void SetCaptcha()
    {
        // Set image
        string s = RandomText.Generate();

        // Encrypt
        string ens = Encryptor.Encrypt(s, "srgerg$%^bg", Convert.FromBase64String("srfjuoxp"));

        // Save to session
        Session["captcha"] = s;
        
        // Set url
        imgCaptcha.ImageUrl = "~/Captcha.ashx?w=305&h=92&c=" + ens + "&bc=" + color;
        // Query strings: c for code(encrypted text), w for image width, h for image height, bc for
        // Background color
    }
    protected void btnSubmit_Click(object s, EventArgs e)
    {
        if (Session["captcha"] != null && txtCaptcha.Text == Session["captcha"].ToString())
        {
            if (success != null)
            {
                success();
            }
        }
        else
        {
            txtCaptcha.Text = "";
            SetCaptcha();

            if (failure != null)
            {
                failure();
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Australia Australia
I'm a Computer Science student at Monash University, Australia.

See my blog at http://blog.farshidzavareh.com

Comments and Discussions