…meie igapäevast IT’d anna meile igapäev…

2008-11-07

Small utils, part 5: netCaptcha, a simple ASP.NET captcha

Filed under: Isiklikud,Programmeerimine — Sander @ 13:40:50
Tags: , ,

netCaptcha.sample

Download NetCaptcha.zip (42KB), includes both code and assemblies.

That the internet is flooded with spam is no news to everyone. Most of it is automated – and so smart people came out with a notion of CAPTCHA: “Completely Automated Public Turing test to tell Computers and Humans Apart.”

I decided to do my own captcha implementation, just for the fun of it. There are far more complicated and better CAPTCHA solutions, reCAPTCHA is probably most widely used.

The only thing that sets my solution apart a bit is the simplicity of its use. Tiny fast 6KB .NET dll, which can be called with just “new NetCapctha(“swordfish”).GenerateImage()” and results a Bitmap that can be saved, manipulated further or streamed to the web page.

However, there are few more constructors for more control:

/// <summary>
/// Initializes a new instance of the <see cref="NetCaptcha"/> class.
/// </summary>
/// <param name="captchaText">The captcha text.</param>
/// <param name="font">The font.</param>   
/// <param name="imageSize">Size of the image.</param>
/// <param name="foregroundColor">Color of the foreground.</param>
/// <param name="backgroundColor">Color of the background.</param>
public NetCaptcha(string captchaText, Font font, Size imageSize, Color foregroundColor, Color backgroundColor)
 
/// <summary>
/// Initializes a new instance of the <see cref="NetCaptcha"/> class.
/// Overloaded, will use default settings with a custom string
/// </summary>
/// <param name="captchaText">The captcha text to use.</param>
public NetCaptcha(string captchaText)
 
/// <summary>
/// Initializes a new instance of the <see cref="NetCaptcha"/> class.
/// Overloaded, will create random string with a custom length
/// </summary>
/// <param name="captchaLength">Length of the captcha.</param>
public NetCaptcha(int captchaLength)
 
/// <summary>
/// Initializes a new instance of the <see cref="NetCaptcha"/> class.
/// Default settings for everything, for a 6-character captcha
/// </summary>
public NetCaptcha()

image In case of last two constructors, I’d really recommend using overloaded public Bitmap GenerateImage(out string captchaText) method, as otherwise you won’t get the random string and the whole idea of having a CAPTCHA becomes somewhat moot…

Together with the CAPTCHA, there is a simple WinForms test project in the archive – which allows you to experiment with netCaptcha and see if it is suitable for your needs – or just find best settings for your website.

Same as all my utils, this is copyleft and free to use everywhere. However, it would be nice if you’d tell me that you have used netCaptcha somewhere. Also, you probably will want to recompile NetCaptcha.dll and sign it with a strong key.

Lisa kommentaar »

Kommentaare veel pole.

RSS feed for comments on this post. TrackBack URI

Lisa kommentaar

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Muuda )

Twitter picture

You are commenting using your Twitter account. Log Out / Muuda )

Facebook photo

You are commenting using your Facebook account. Log Out / Muuda )

Google+ photo

You are commenting using your Google+ account. Log Out / Muuda )

Connecting to %s

Create a free website or blog at WordPress.com.