Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Adding CAPTCHA support to Cuyahoga ContactUs Module

, 30 Sep 2010
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ali Ozgur
Team Leader PragmaTouch
Turkey Turkey
- Software developer
- Has BS degree in Computer Engineering
- Has MBA degree
- Programmed with C, C++, Delphi, T-SQL and recently C#
- Little educational experience with Prolog
- Feel enthusiasm about NHibernate and LINQ
- Love to develop on Cuyahoga Web Framework
- Developer of PragmaSQL Editor
(Code Project Members Choice Winner for 2009 and 2010)
- Developed JiraTouch and MoodleTouch for iPhone
- PragmaTouch Lead (www.pragmatouch.com)
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 1 Oct 2010
Article Copyright 2010 by Ali Ozgur
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid