Click here to Skip to main content
12,753,195 members (39,455 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


47 bookmarked
Posted 3 Aug 2010

Simple CAPTCHA, Create your own in C#

, 4 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Create your own simple captcha in C#

Have you ever wondered how to create a Captcha for your web forms? Well, it's really simple and it won't take more than 100+ lines of code. This sample captcha that I will demonstrate will have random Font Type, Font Style, Font Color, Font Rotation, Background Style and Background Colour. Here are some samples of what this can generate:

Now without too much explanation, here it goes.

Step 1: In your web project, add a Generic Handler that's the file with the .ahsx extension. Ill just use this for this demo for simplicity and also it will compile on demand like a normal aspx file. It's up to you if you want to create a DLL which is better so you can reuse it in any projects you want. To do that, right click on the project on your Solution Explorer.

Then choose your file type, in this sample it's a Generic Handler.

Step 2: Once added you can start coding, here are the sample codes which you can freely copy.

<%@ WebHandler Language="C#" %>

There is some commented explanation along the way so you won't get lost, but you can still ask me if you really need.

Step 3: Use that Captcha. On your aspx, add the following as an image like such:

<asp:Image ID="imgCaptcha" ImageUrl="Captcha.ashx" runat="server" />

If you noticed, it's like you are using it as an image, this is because it's an image rendered by that handler which you can see on the bottom part of the Captcha code.


Step 4: Assign a text on that captcha on your code behind:

private void SetCaptchaText()
 Random oRandom = new Random();
 int iNumber = oRandom.Next(100000, 999999);
 Session["Captcha"] = iNumber.ToString();

Note: You may notice that I am using only numbers in this regard, this would be easy so users cannot be confused with l and 1, 0 and o, and other similar looking characters.
Note: By setting the session, you are also setting the text to render on the captcha and if you look at the captcha codes, it is in this section.

string sCaptchaText = context.Session["Captcha"].ToString();

Step 5: Compare that value to what the users had keyed into your text box:

if (Session["Captcha"].ToString() != txtCaptcha.Text.Trim())

That's it. Congratulations! You have created your own captcha.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand

You may also be interested in...


Comments and Discussions

GeneralError Pin
lazybit2-Oct-10 2:13
memberlazybit2-Oct-10 2:13 
GeneralRe: Error Pin
Raymund Macaalay2-Oct-10 14:39
memberRaymund Macaalay2-Oct-10 14:39 
GeneralRe: Error Pin
lazybit2-Oct-10 18:19
memberlazybit2-Oct-10 18:19 
GeneralRe: Error Pin
Raymund Macaalay3-Oct-10 22:06
memberRaymund Macaalay3-Oct-10 22:06 
GeneralRe: Error Pin
jaspadee11-Jan-13 3:49
groupjaspadee11-Jan-13 3:49 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 4 Aug 2010
Article Copyright 2010 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid