Implementing Captcha in a DotNetNuke Module 

Recently I was working on a project for a client of mine where I needed to implement a Captcha within a DotNetNuke module.  Now in the past I have implemented Captchas on standard ASP.NET sites but I had yet to work with them in DotNetNuke.  Now, DotNetNuke does have a built in Captcha control which can be used on login/registration, but there doesn't appear to be much information out there about how to implement it in your own projects.  I wanted to provide a quality solution for my client by matching the standard DNN look and feel so I decided to take a dive into the core code and find out just how you can use the Captcha control in your own module development projects.  This article will first quickly explain how I went about finding this information and then will discuss the very simple steps needed to add a captcha to your project!

The Research

Knowing that the captcha is an available option for the registration page I started by looking in the /Admin/Security/Register.ascx page to see if I could find the code implemented there.  This ended up turning up a few loose ends as the User.ascx control was used to provide a good majority of the code for the page.  So the next step was to look at the /Admin/Users/User.ascx file, in there I found the key that I needed, the Captcha is a compiled control that is part of the DotNetNuke.UI.WebControls namespace in the DotNetNuke assembly.  Now, I have an example that I could work with to put the Captcha in my page.

The Implementation

Now that I found the information it was very easy to implement the Captcha in my project.  There are a total of 3 pieces of code that you must insert into your project to successfully use the Captcha control.  Each of these items will be provided and detailed below.

Register the Assembly

The first step is to add the proper register declaration to your user control.  This statement is what allows you to later declare and use the Captcha control in your page.  You can simply copy and paste this code to the top portion of your .ascx file.

<%@ Register TagPrefix="dnn" Assembly="DotNetNuke" Namespace="DotNetNuke.UI.WebControls"%>

Declare the Control

The next step is to actually place an instance of the control in your module. Below is an example of a default configuration of the control, many other options exist for you to style the module to meet your needs, you can also implement a ResourceKey to allow localization of the error message.

<dnn:CaptchaControl ID="ctlCaptcha" CaptchaHeight="40" CaptchaWidth="150" 
                    ErrorStyle-CssClass
="NormalRed" cssclass="Normal" runat="server" 
                    ErrorMessage
="The typed code must match the image, please try again"/>

As you can see this is a pretty simple step to complete, I have set the height and width of the captcha image to ensure that it fits nicely inside my area on the control, I also was sure to set the ErrorStyle and CssClass attributes to ensure that everything displays nicely with my skin.  You have other options that you can configure here as well, including the number of characters and other items of that nature.

Validation of Captcha

The final step is to implement code in the backend that validates the Captcha to ensure that the user supplied information was correct.  Unlike other validation controls the Captcha does NOT validate prior to postback so this step is mandatory to ensure that the users supplied the Captcha.  Below is some sample C# code to validate the Captcha, just a simple if statement.

if (ctlCaptcha.IsValid)
{
    
//Do your processing!
}

Conclusion

Even though somewhat undocumented in the DotNetNuke core system the DNN core provides a very easy to implement Captcha that you can use in all of your module development projects.  Please post your comments below, if you have technical questions about this article please use my forum for assistance.

Posted by Mitchel on Sunday, January 27, 2008
 

Comments

Hi Mitchel,
wish that post was written a week or two ago, would have saved me a day of searching :)
I took a hint from the feedbacks module on how to do it.
But I have a question - in design mode it shows "Error rendering control" yet it is working - should I ignore the error or I'm missing something?

By evgamon on Monday, January 28, 2008 at 12:49 AM

Thanks. Interesting a few month/versions a go I think "captch" was a "built in" option which some how got lost.

By Avi on Monday, January 28, 2008 at 3:21 AM

Evgeni,

Yes, I get the same error, it appears that they have an error in the code for proper design time rendering.

I have tested and not found any ill-effects of using it in a production module so I would just ignore the error.

By mitchel.sellers@gmail.com on Monday, January 28, 2008 at 3:33 AM

One more thing that puzzles me with the captcha - how can I change the font of the captcha, the cssclass applied only for the text above the text box. Is it possible at all? Sorry if it's a silly question, I'm still learning.

By evgamon on Monday, January 28, 2008 at 11:46 AM

It would be more involved than a CSS class as the letters are actually rendered onto the image via the custom control.

I am not infront of a DNN install at the moment so I cannot look at the properties available, but you might have a look around at the available properites that you are not specifically setting.

By mitchel.sellers@gmail.com on Monday, January 28, 2008 at 12:27 PM

As usual, a great blog topic!

By Will on Wednesday, January 30, 2008 at 4:21 AM

I'd love to know how to change the style of the generated graphic. I've seen other captchas with code that lets one set the number of digits and how messy it looks and that sort of thing; so if anyone knows how to change it for the core captcha, please let us know.

By Rob on Thursday, February 07, 2008 at 1:27 AM

Rob,

You can change some of the items such as the number of characters using the other properties exposed for the control. However, in my testing I didn't find any way to change the default behavior for complexity, distortion, or noise which are typically provided with Captcha systems.

By mitchel.sellers@gmail.com on Thursday, February 07, 2008 at 5:11 AM

Thanks for the tip Mitch, I had not realised how easy this was to implement and added it to our Referral module (recently some auto-script sent 1400 emails from our Refer a Friend form on PokerDIY.com..)

By Rodney Joyce on Monday, March 10, 2008 at 1:07 AM

DNN default captcha has a problem with upper/lower case, has anybody seen this behavior ??
Whatever the upper/lower case you use for entering the letter, it is not taken into account. Of course, if you do not enter the correct text, it does not work.

DV

By Declic Video on Tuesday, March 25, 2008 at 11:59 PM

Google, find your HowTo, implement it, that`s it.

Thank you for this posting ...

By Torsten Baumelt on Tuesday, April 22, 2008 at 1:37 AM

Nobody noticed the problem with Captcha I mentionned earlier ??

DV FX

By Déclic Vidéo FX on Tuesday, April 22, 2008 at 7:11 AM

DV,

Sorry I missed your previous comment. I have noticed at at times it is hard to tell what characters are supposed to be, but it works in all cases that I have tried it.

If you have a specific example that doesn't work, post it to the forum and I'll try to help you sort it out.

By mitchel.sellers@gmail.com on Tuesday, April 22, 2008 at 8:04 AM

Hello,

I should have badly explained. We have noticed (as reported also in Ventrian forums where Captcha has been implemented for NewsArticle - using core captcha), that whatever the upper/lower case you put, it is not taken into account.
Here is an example to better understand. Captcha show the image HgtV5
If you put hgtv5 or HGTV5 or hGTv5 or whatever else, it works. It means that it is not case sensitive, this is strange, isn't it ? Already reported to core team ??

DV FX

By Declic Video on Tuesday, April 22, 2008 at 8:46 AM

I have tested on 4.6.0 and don't have that issue.....(I don't have other versions handy at the moment to test).

Which version of DNN have you noticed this on?

By mitchel.sellers@gmail.com on Tuesday, April 22, 2008 at 9:28 AM

I have tested this on:
- DNN 4.5.5 (live)
- DNN 4.8.2 (live)
- DNN 4.8.2 (local)
A friend tried on his 4.8.2 and another one who is still in 4.5.5 has also tested, same problem (and it is also reported by other users in Ventrian forum - seems to be linked to Core captcha). Nevertheless, this is maybe not really important, I sent weeks ago an email to core team to inform them, asking if it was a security concern, I never receive any reply. So, probably not important, but was just to mention.

DV FX

By Déclic Vidéo FX on Tuesday, April 22, 2008 at 10:11 AM

Wow! Maybe I just have a version that works.....

Actually from a true security standpoint this is an issue, the idea behind a captcha is that it should be hard for a computer to read the characters.....if case doesn't matter, that reduces the complexity....making it easier for someone to automate guessing the CAPTCHA

By mitchel.sellers@gmail.com on Tuesday, April 22, 2008 at 10:15 AM

So, that's strange that I never had any feedback from security core team. Well, it does not matter a lot for me.
Just to show you that I am not alone with this problem:
<a href="http://www.ventrian.com/Support/ProductForums/tabid/118/forumid/4/tpage/2/view/topic/postid/28792/Default.aspx#29708">http://www.ventrian.com/Support/ProductForums/tabid/118/forumid/4/tpage/2/view/topic/postid/28792/Default.aspx#29708</a>

DV

By Déclic Vidéo FX on Tuesday, April 22, 2008 at 4:06 PM

Great Article. You have saved me a few hours of coding

By Chris Evans on Friday, August 29, 2008 at 8:35 AM
Click here to post a comment

Donate

Show your appreciation for the content/modules made available by MitchelSellers.com by making a donation. Donations are used to assist with dedicating time to creating free content.