Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Adding CAPTCHA support to Cuyahoga ContactUs Module

0.00/5 (No votes)
30 Sep 2010 1  
In this article, I will give you tips on adding reCAPTCHA to Cuyahoga ContactUs Module.

PragmaTouch products mTouch and mTouch+ (native iOS app for Moodle) have an increasing popularity and we get lots of spam emails through our "Contact Us" page. As a result, using some sort of CAPTCHA was inevitable although I think CAPTCHAs are not user friendly.

After evaluating some CAPTCHA implementations, I decided to integrate reCAPTCHA to our Cuyahoga powered site. Here are the details.

Get Public and Private Keys for your Site

You will need a Google Account to create Public/Private keys to access reCAPTCHA services. After logging in to Google, go to reCAPTCHA site administrator page and create the keys for your site.

CASE #1: You do not have the Source Code of ContactUs Module

  1. Download the reCAPTCHA ASP.NET Library
  2. Copy Recaptcha.dll to ~/bin folder
  3. Insert the following code at the top of your ContactUs.ascx found under ~/Modules/ContactUs folder:
    <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
  4. Insert the reCAPTCHA control into the ContactUs.ascx found under ~/Modules/ContactUs folder.
     <recaptcha:RecaptchaControl
        ID="recaptcha"
        runat="server"
        PublicKey="your_public_key"
        PrivateKey="your_private_key" />
  5. Remove ValidationGroup attribute of all ASP.NET controls found in your ContactUs.ascx. This is required because reCAPTCHA ASP.NET Library is ASP.NET 1.1 compatible and does not support validation groups.

This case has just one drawback. Your users will be displayed a success message even though they were not able to send the email. This is because developers of ContactUs module preferred to display the success message even though the page is not valid. Here is the code:

private void btnSend_Click(object sender, System.EventArgs e)
{ 
      // Send email
      if (this.Page.IsValid)
      {
        try
        {
          Site site = base.PageEngine.ActiveNode.Site;
          _module.Send(this.lstSendto.SelectedValue, site.WebmasterEmail,
              this.txtSubject.Text,
              String.Format("To: {0}\r\nFrom: {1}\r\nName: {2}\r\n{3}", 
		this.lstSendto.SelectedItem.Text, this.txtFromemail.Text, 
		this.txtFromname.Text, this.txtMessage.Text));          
        }
        catch
        {
          this.lblSendError.Text = base.GetText("FAILDTOSEND");
          this.lblSendError.Visible = true;
        }
      }
	  
      this.pnlContactus.Visible = false;
      this.pnlMailsend.Visible = true;
      this.lblMailsend.Text = String.Format(base.GetText("MAILSENDOK"), 
				this.txtFromname.Text);
 }

Note to Lines 21-23. As you can see, success message is sent regardless of Page.IsValid == true.

CASE #2: You have the Source Code of ContactUs Module

  1. Download the reCAPTCHA ASP.NET Library
  2. Add reference to Recaptcha.dll in the Cuyahoga.Modules.ContactUs project
  3. Insert the following code at the top of your ContactUs.ascx found under the Web folder of the project:
    <%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
  4. Insert the reCAPTCHA control into the ContactUs.ascx found under the Web folder of the project:
     <recaptcha:RecaptchaControl
        ID="recaptcha"
        runat="server"
        PublicKey="your_public_key"
        PrivateKey="your_private_key" />
  5. Remove ValidationGroup attribute of all ASP.NET controls found in your ContactUs.ascx. This is required because reCAPTCHA ASP.NET Library is ASP.NET 1.1 compatible and does not support validation groups
  6. Modify btnSend_Click code block found inside ContactUs.ascx.cs:
     private void btnSend_Click(object sender, System.EventArgs e)
     { 
          // Send email
          if (this.Page.IsValid)
          {
            try
            {
              Site site = base.PageEngine.ActiveNode.Site;
              _module.Send(this.lstSendto.SelectedValue, site.WebmasterEmail,
                  this.txtSubject.Text,
                  String.Format("To: {0}\r\nFrom: {1}\r\nName: {2}\r\n{3}", 
    		this.lstSendto.SelectedItem.Text, this.txtFromemail.Text, 
    		this.txtFromname.Text, this.txtMessage.Text));
              
              this.pnlContactus.Visible = false;
              this.pnlMailsend.Visible = true;
              this.lblMailsend.Text = String.Format(base.GetText("MAILSENDOK"), 
    				this.txtFromname.Text);
            }
            catch
            {
              this.lblSendError.Text = base.GetText("FAILDTOSEND");
              this.lblSendError.Visible = true;
            }
          }
          else
          {
            this.lblSendError.Text = "ERROR: Check if all required fields 
    		has data and you typed the words displayed 
    		in the image below correctly.";
            this.lblSendError.Visible = true;
          }
     }
  7. Copy Cuyahoga.Modules.ContactUs.dll to ~/bin directory of your site
  8. Copy ContactUs.ascx to ~/Modules/ContactUs folder of your site

Note to else condition starting from Line 24. We modified the event handler code to avoid displaying success message to our user in case of invalid page.

ContactUs Module with reCAPTCHA

Here is a screen shot of our reCAPTCHA enabled ContactUs module.

contactus.PNG

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here