Stopping Orchard Contact Spam

April 12, 2018

Latest Post The Hound of the Baskervilles by Owen Soule

Tired of Russian bots hammering your ridiculously outdated contact page code? Well you've come to right place. In a thinly veiled attempt to add content without creating any artwork, I've decided to explain how I added Google's Recaptcha to the Orchard CMS module PlanetTelex.ContactForm. A strangely specific post that will have a target audience of right around nobody, let's begin!

souledesigns-contact-not-bot-friendly.jpg
Sorry, Russian bots, you can no haz :-(

A little background, I haven't really updated my Orchard CMS (the blog engine this site runs on) codebase in a few years and I don't plan on doing so until it blows up. So when I do make updates it's almost always to resolve annoying issues like contact page Russian bots or an onslaught of blog comment Cilais/Air Jordan offers. While the blog comment fix was pretty easy using built in Orchard tools, this change required a bit of code hackery. For all the files you can checkout the module source on my Github repo, the updated files include:

Controllers\ContactFormController.cs  Drivers\ContactFormDriver.cs  Services\ContactFormService.cs  Services\IContactFormService.cs  Views\DisplayTemplates\Parts\ContactForm.cshtml  

All those paths are beneath the PlanetTelex.ContactForm root folder. Also, the ContactForm.cshtml view is very basic and only does server side validation. I have a customized version in my SouleDesignsDark theme that does client side validation and is bootstrappy, if you're into that kind of thing.

I won't delve into the programming details any further because it would be painfully boring and time consuming. Let's just say I recreated the recaptcha logic from the built in Orchard.AntiSpam module in the PlanetTelex.ContactForm module. Code duplication is a horrible programming practice and I urge you not to follow in my footsteps! However, if you choose to do so you'll need to:

If you need more details on the second two points checkout my Stopping Orchard Comment Spam post where I outline this process in detail. Now I know you're asking, "Why do I need to setup a different module to get this contact form recaptcha working?" Well, I opted to reuse the settings from Orchard.AntiSpam rather than forcing you to reenter them for the contact form. This is probably a kluge, just like my hackey code duplication, but I don't care!

Owen Soule

Published April 12, 2018