Blogging: Controlling SPAM in Orchard CMS with OpenCaptcha

By Art on February 16, 2012

Edit (03-Apr-2017) Orchard has moved on a lot since this was written and as such it is somewhat out of date. I have removed the links to the example code, but left the blog here for prosperity

When I started blogging last year, my first major decision was either to host my own site somewhere using a CMS type system or use an online blogging service like blogger for example. Ultimately, because I wanted to have quite fine grained control over my blogs and also the space to expand into other areas, I choose the hosting route and because I had heard good things about Orchard CMS, and the fact that it can use SQL CE as its database, I also opted to install Orchard v1.3.9

Everything went well and within a short space of time, I had the site working and was able to start blogging. Naturally, as part of the blogs I wanted the community to have the ability to feedback their thoughts so a comments section was essential. Orchard has its own very simple comments module (Orchard.Comments) or you can enable the Disqus module which is lot more flexible and professional looking. So I signed up for a Disqus account and fired up the disqus comments module and again everything went very well. Or at least it was until I upgraded Orchard to v1.3.10.

This version of Orchard provides the ability to preview your content before publishing which is something I really wanted to do with my blogs. However, the disqus module didn’t seem to be quite ok with this new version and it muddled up my comment threads from different blogs. I wasn’t able to resolve this in a short space of time so I took the decision to remove the disqus module and use the much simpler Orchard Comments module instead.

Within a few weeks of this module being live, I started to get hit by spam comments and although the comments module has an akismet spam feature, it only serves to mark a post as spam rather than preventing the post entirely. After a while of playing around with the comments module, I realised that I probably had just enough C# skills to actually modify the module to my own liking by incorporating a simple captcha type feature which I’d hope will put an end to most of the spam.

There are a number of captcha online services which some are free, some aren’t and in the end I chose a simple implementation from OpenCaptcha. The service comprises of a two phase process where during the first phase you generate a unique key yourself and then request an image from the service using this key. All of this logic I was able to easily implement into the Parts.Comments.cshtml file.

The next phase is the challenge phase and OpenCaptcha provides another url where you pass in the unique key that you generated and the response from the user. It will then return a simple pass/fail text response. Hooking this up into the module was much harder for me as although I have heard a bit about MVC and Razor and the like, I have never actually used them! So this was a bit of a steep learning curve for me, but in the end I had worked out what models and controllers that needed updating and got the publishing event checking OpenCaptcha. I was also able to ensure that the submission of an invalid comment works as planned as I think that broke with v1.3.10 as well as it used to redirect to the home page of the site and not back to the blog that you were posting the comment on.

So all in all, I was quite pleased with the outcome and even found time to source a javascript function that allows the image to be reloaded to a new captcha without refreshing the page. Likewise, this post has also been a pleasant diversion from my usual content on SQL.

If anyone is interested in the updates that I have made to the Orchard.Comments module files then you can download them from [redacted]. Feel free to do as you wish with them. However, bear in mind that I’m not a web developer, I’m a SQL Server developer, but I do know a bit of C#/VB to help me out with certain projects. As such I may not have written the best C# code ever into this module so if you do download the code, take it for what it is and does. This has solved a minor issue with me and I enjoyed learning a bit of how Orchard hangs together and I’m happy to share the updated Orchard.Comments files with you.

Enjoy!