I’m currently working on a little experimental Castle MonoRail project and wanted to add a CAPTCHA image to a form. Of course I could try to create my own but I don’t like reinventing the wheel so I did a little digging and came across reCAPTCHA. It can be integrated into websites and has a number of tools available to help you do this.
I couldn’t find an example of using it with MonoRail, but did find this example for ASP.NET MVC by Derik Whittaker which pointed me in the right direction. Here’s what I did:
1. Sign up to reCAPTCHA to obtain my private and public keys. I made this a global account so I can test it on my local server rather than only on an assigned domain.
2. Added the keys to my web.config:
3. Downloaded the ASP.NET library and added a reference to the recaptcha.dll assembly.
3. Created a new helper for rendering the control in the view:
4. Added the helper to my controller:
5. Called the helper inside a form in my view:
6. Created a new parameter binder (instead of Derik’s filter) that determines if the user entered the CAPTCHA code correctly:
7. Used the new parameter binder on the receiving controller action to determine if the user passed the CAPTCHA test:
And that’s about it. I’ll be releasing this project as open source soon (once I’ve made some more progress). I’ve added these components to my AndyPike.Commons assembly which will also be available to those that would like it, but the above code samples should be enough to get you going. Oh, and here is what the view looks like: