Tuesday, 5 July 2011

ReCAPTCHA for DotNetNuke Forums

This post walks through implementing Googles reCAPTCHA control into a DotNetNuke Forums web site.  It seems like it would be pretty straightforward, but there are some hurdles along the way.

These instructions are specific for DNN Forums, but you can use the same techniques for other DNN modules.

Prerequisites

  1. A DNN website with the DNN Forum module installed.
  2. The DNN Forum source code (I'm working with release 5.00.01).
  3. Basic VB.NET programming skills.
  4. Basic ASP.NET skills.


The first step (the easy part), is to edit the post page so that it contains the reCAPTCHA control.
  1. Visit the reCAPTCHA website, create an account and generate the requisite keys.
  2. Download recaptcha.dll and place it in your sites bin directory.
  3. Add the following line to the top of the Forum_PostEdit.ascx:
    <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
    
  1. In Forum_PostEdit.ascx, locate the <tr id="rowModerate" ...> element, and immediately after, insert a new <tr> containing the recaptcha control:
    <tr>
        <td align="center" width="100%">
            <recaptcha:RecaptchaControl ID="recaptcha1" runat="server" Theme="red" 
            PublicKey="[YourKey]"
            PrivateKey="[YourKey]" />
        </td>
    </tr>
    

Now we will add logic so that the post does not get submitted if the reCAPTCHA is not complete.
  1. Open the DotNetNuke.Forum project in Visual Studio.
  2. Add a reference to recaptcha.dll.
  3. In Forum_PostEdit.ascx.vb, add the following line somewhere to the class body:
    Protected WithEvents recaptcha1 As Recaptcha.RecaptchaControl
    
  1. Find cmdSubmit_Click(...), and add the following code to the start of the method:
    If Not recaptcha1.IsValid Then
        Return
    End If
    
At this point, the reCaptcha will appear on the page, and will prevent the user from proceeding to submit the post until they have completed the CAPTCHA correctly.
The problem is that the user only gets one try - after the first attempt, the reCAPTCHA control will disappear.
This is a side effect of the UpdatePanel used in DNN to prevent the entire page from being posted when the user clicks post.
To work around this problem, we will now add a PostUpdateTrigger to the UpdatePanel to force a full PostBack when the user clicks Submit.
  1. Add the following two functions to Forum_PostEdit.ascx.vb:
    Private Function findUpdatePanelParent(uc As Control) As UpdatePanel
        If TypeOf uc Is UpdatePanel Then
            Return DirectCast(uc, UpdatePanel)
        End If
        Return findUpdatePanelParent(uc.Parent)
    End Function
    
    Private Sub registerFullPostback()
        Dim updatePanel As UpdatePanel = findUpdatePanelParent(Me)
        Dim trig As PostBackTrigger = New PostBackTrigger()
        trig.ControlID = "Forum_PostEdit$cmdSubmit"
        updatePanel.Triggers.Add(trig)
    End Sub
    
    

  1. Locate Page_Init(...), and add the following line:
    registerFullPostback()
    
We're nearly done. All you need to do now is recompile DotNetNuke.Forum, and copy DotNetNuke.Forum.dll to your websites bin directory.

No comments:

Post a Comment