Implementing the DotNetNuke CAPTCHA Control

Excerpt from  Professional DotNetNuke Module Programming

By Mitchel Sellers

With the exception of Label and a handful of other controls, all DotNetNuke Web controls are contained as compiled controls inside the DotNetNuke.UI.WebControls namespace. Therefore, a single Register tag can handle registering them for use. This Register tag must be added at the top of any .ascx control that needs to use the controls.

As illustrated by the following, the code is the same for C# and VB:
asp Code:
<%@ Register TagPrefix="dnn" Assembly="DotNetNuke"
This single Register tag grants access to all controls inside the DotNetNuke.UI.WebControls  namespace.  Inside the modified.ascx, if you now type <dnn:, you will see IntelliSense information on all available controls. The following sections take a look at the implementation of a few controls.

One of the most commonly used DotNetNuke Web controls is the CAPTCHA control. (The word “CAPTCHA” is actually an acronym standing for “completely automated public Turing test to tell computers and humans apart.”) This control provides a form of human verification, and in the case of the Guestbook module, it is designed to prevent bots (computer robots) from posting to the guestbook. The CAPTCHA control is very easy to implement after the Register tag has been added to the top of the SignGuestbook.ascx control.

Listing 12-1 shows the code that should be added to the control to define the row containing the CAPTCHA.

Listing 12-1 CAPTCHA Definition
asp Code:
<tr runat=
"server" id="trCaptcha">
        <dnn:Label ID=
"lblCaptcha" runat="server"
":" />
        <dnn:CaptchaControl runat=
"server" CssClass="normal"
"5"  />
    <td colspan=
Table 12-1
This code sample shows a very minimal configuration of the CAPTCHA control, with the specification of a total number of characters. Table 12-1 provides a list of other helpful properties that can be set on the CAPTCHA control.

Property Name Description
CaptchaChars The set of characters that could be used to create the CAPTCHA
CaptchaHeight Height of the CAPTCHA
CaptchaLength Total number of characters to use in the CAPTCHA
CaptchaWidth Width of the CAPTCHA
CssClass The CSS class used by the control
Text The prompt Text used

With the CAPTCHA control added to the Guestbook module, you can now hook it up on the back end. Inside the Page_Load method of the SignGuestbook module, you must validate the CAPTCHA. Once you have established the settings, you set the visibility on the CAPTCHA control row. To do this, add the following code to the Page_Load method. Listing 12-2 shows the C# implementation and Listing 12-3 shows the code additions for the VB module.

Listing 12-2 C# Code Addition for Page_Load
csharp Code:
bool allowAnonPosting = bool.Parse(allowAnon.ToString());
bool useCaptcha =
Visible = useCaptcha;
if(UserId == –1 && !allowAnonPosting)
Listing 12-3 VB Code Addition for Page_Load
vbnet Code:
Dim allowAnon AsBoolean
allowAnon =
("WROX_Guestbook_AllowAnon"), String))
Dim useCaptcha AsBoolean
useCaptcha =
(CType(Settings("WROX_Guestbook_EnableCaptcha"), String))
Visible = useCaptcha
If UserId = –1 AndAlso Not allowAnon Then
The final step of the process is to modify the code inside the btnSign_Click event to check for the CAPTCHA’s IsValid property (in addition to Page.IsValid) if the CAPTCHA is enabled. This is done by adding the code shown in Listing 12-4 (C#) or Listing 12-5 (VB) to the modules. This is a simple conditional check. Now the module can be secured from malicious use.

Listing 12-4 C# CAPTCHA Check Code
csharp Code:
bool useCaptcha = bool.Parse
if(Page.IsValid &&
(!useCaptcha  || (useCaptcha && ctlCaptcha.IsValid)))
Listing 12-5 VB CAPTCHA Check Code
vbnet Code:
Dim useCaptcha AsBoolean
useCaptcha =
(CType(Settings("WROX_Guestbook_EnableCaptcha"), String))
If Page.IsValid AndAlso (Not useCaptcha Or
(useCaptcha And ctlCaptcha.IsValid))Then
Overall, this is a very simple control to use. After building the changes and enabling the CAPTCHA for the guestbook, you can see a new control on the page prompting for the input of the code. This control must be used for any system that accepts input from unauthenticated users.


One response to “Implementing the DotNetNuke CAPTCHA Control”

  1. Anonymous says:

    Now you can create one of three CAPTCHA controls:

    DotNetNuke CaptchaControl
    Telerik RedCaptcha

    Please, review: Tutorial, Live demo
    Anton Burtsev.

Leave a Reply

Your email address will not be published. Required fields are marked *