This project is read-only.


Topics: Customizing Orchard, General, Writing modules
Mar 14, 2011 at 4:20 AM
Edited Mar 14, 2011 at 4:24 AM

Is anyone out there working on a module to allow CAPTCHA?
Right now I need one on the Registration page so I am overriding the Register.cshtml view to allow for reCAPTCHA.
However, I think CAPTCHA should be composable with other parts. It seems relevant that adding it to comments, blog posts, registration, etc would be worth while so I am guessing this is something better done in a module that other parts could then attach?

Any pointers where to start down that path instead of just adding it directly to my customized Registration.cshtml?
AFAIK, there are no data dependencies here so a simple "module?" without and parts or records? I'm still very unfamiliar with Orchard terminology so forgive me if I use the wrong nouns. :)

I'm more than happy to implement and donate the code. Just need to find the proper document or sample module to mimic.


Mar 15, 2011 at 7:26 PM

I've looked in to this a bit and I think it would work, but not as a normal module.
To use CAPTCHA in MVC you have to apply an attribute to the HttpPost action that you want to enforce CAPTCHA on. The turorial I used also create an HtmlHelper for rendering the captcha in the view.
The only way to have access to that attribute and htmlhelper is either write it in your own project or reference it.
I'm guessing having one module reference another is bad form so the best thing, IMO, would be to include captcha support in Orchard.Core or Orchard.Framework (wherever most appropriate).
It can still be configured in module form so that the settings for public/private key and theme can be controlled from the dashboard on an admin screen for the module.

Is this something the Orchard team would consider adding to the core? Or do you think there is a way to add this functionality as a normal module?


Mar 15, 2011 at 7:48 PM

Can you explain why you believe it needs to be in core?

Mar 16, 2011 at 12:32 AM
From the examples I have seen you use a custom attribute [captchaValidator].If this exists in another module, how would you be able to use it without actually referencing that other module. I didn't think module to module references were a good idea.

There is probably a way to do this by injecting the validator but it wasn't immediately obvious to my how to set that up.

My thought was based purely on assumption, which was my primary motivation for posting about it.

If I can find a way to do it via injection the. I can roll the module pretty quickly. I'll keep you posted on my findings. If you have any ideas, please share. :)


Sent from my iPhone

On Mar 15, 2011, at 2:48 PM, "bertrandleroy" <> wrote:

From: bertrandleroy

Can you explain why you believe it needs to be in core?

Mar 16, 2011 at 12:34 AM

Right: why would you use an attribute? It seems like that is the bad design decision.

Mar 16, 2011 at 6:41 AM

Ok, I've got it working as a module. Currently I have this implemented.
Site Settings exposes PublicKey, PrivateKey and Theme settings.

I have implemented a CaptchaService (ICaptchaService) that is injecte into any module controller that needs it (you do have to reference orchard.Captcha project to get the interface -- I could find no better way to do this).
To render the captcha in a view you just have to get the value from _captchaService.GenerateCaptcha() into your view. Right now I just assign it to ViewData["Captcha"] and render it like this @T(ViewData["Captcha"] as string )
Alternately you can create a model that exposes it (however you want to do it in your module).

Then to validate the form, on any HttpPost actions where captcha is necessary just create the ActionResult with a FormCollection parameter (along with any other params you need) then call this
if(_captchaService.IsCaptchaValid(form, Request.UserHostAddress))
{ //handle it your way; }

Other stuff:
I have implemented a version of CaptchaService.GenerateCaptcha that takes no parameters so the theme is read from settings
I have implemented a version of CaptchaService.GenerateCaptcha that takes a theme name so you can override the theme per page if you want.
    I plan to implement defaults so that if you pass in an invalid theme name then the captcha will default to "red".
I have implemented a version of IsCaptchaValid that takes a FormCollection and userHostAddress string.
I have implemented a version of IsCaptchaValid that takes captchaChallengeValue, captchaResponseValue and userHostAddress for those who don't want to pass in the entire FormCollection
Currently, if you embed a captcha on your page using my module and you haven't configured the module settings then GenerateCaptcha will return the message "CAPTCHA Part Record Was Not Found".
    If you are rendering the results of this to your screen then you should see that message in your browser instead of the captcha control.

Future Plans:
I want to enhance the settings so that only valid themes can be saved.
I want to enhance the module to allow complete theming. In reCaptcha this is done by setting the theme to "custom" and applying your own css.
I want to provide an additional setting that is the css to apply to the captcha when the theme is set to custom. -- Not quite sure how I will do this yet so tips would be appreciated
I want to make sure the captcha works correctly when on a localized site.
I would like to implement functionality to support other types of captchas but this won't be done for a while as I need support for plain-old-recaptcha only.

I have named the module Orchard.Captcha since I didn't want to brand it with my own company name. Is this OK or should I rename it so as not to cause confusion about what team developed it? All I care about is that my name remains somewhere in the Author section. :)

Any requests?

bertrandleroy -- thanks for steering my in the proper direction, this was definitely a learning experience and I now have even more respect for Orchard. :)