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
- Download the reCAPTCHA ASP.NET Library
- Copy Recaptcha.dll to ~/bin folder
- Insert the following code at the top of your ContactUs.ascx found under ~/Modules/ContactUs folder:
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
- 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" />
- 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)
{
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
- Download the reCAPTCHA ASP.NET Library
- Add reference to Recaptcha.dll in the
Cuyahoga.Modules.ContactUs
project
- 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" %>
- 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" />
- 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
- Modify
btnSend_Click
code block found inside ContactUs.ascx.cs:
private void btnSend_Click(object sender, System.EventArgs e)
{
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;
}
}
- Copy Cuyahoga.Modules.ContactUs.dll to ~/bin directory of your site
- 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.