Async Recaptcha for BlogEngine.NET 1.6
The instructions on this page are no longer valid for the latest version.
I really am getting sick of the spam that is appearing on this blog, so yesterday I decided to implement Recaptcha for BlogEngine.NET. I had three main goals:
- Easy to implement – as any control, it needs to be pretty trivial to add it to an existing blog instance
- Configurable via the Extensions panel – I really like what BlogEngine has done with their ability to edit extensions, so I wanted the control to be configurable in this way
- It had to be asynchronous – when a user posts a comment, I don’t want the whole page to refresh
The final solution is pretty close to what I had in mind. The extension is a single file that needs to be inserted into the ‘/App_Code/Controls’ folder. In addition, two lines of code need to be added/modified in the ‘/User controls/CommentView.ascx’ file.
Just a quick note to anyone who may want to implement this on your own. I have noticed that once in a while, when using IE, I get an error that BlogEngine is not defined and the error points to the following file: ‘/js.axd?path=%2fadmin%2fwidget.js&v=22.214.171.124’. The changes involved shouldn’t have any effect on this, but I am not 100% sure if this is due to my code or not. If it is, hopefully I’ll figure it out in the next day or so, as I’m way to tired right now. [ this should now be fixed in the 0.95 version ]
To implement the Recaptcha control, the following steps need to be taken:
- Download Recaptcha.zip, extract the file in it, and place the file in the /App_Code/Controls folder.
- Open ‘/User controls/CommentView.ascx’ in the editor of your choice and put in the following line at the spot where you would like your control to appear. On this blog, the code is right above the submit button when posting comments.
<blog:RecaptchaControl ID="recaptcha" runat="server" TabIndex="8" /><br />
In the example above, I also put in an extra line break for some extra white space. I have also put in a TabIndex ( and changed the TabIndex of the submit button ).
- The onClick event on the actual submit button needs to change. If you have not modified the code at all, the submit button should look like this:
<input type="button" id="btnSaveAjax" value="<%=Resources.labels.saveComment %>" onclick="return BlogEngine.validateAndSubmitCommentForm()" tabindex="7" />
For the recaptcha code to work, the submit button needs to change to the following:
<input type="button" id="btnSaveAjax" value="<%=Resources.labels.saveComment %>" onclick="return validateWithRecaptcha()" tabindex="9" />
That’s it. No recompile is needed, and the Recaptcha control should be functional. If you go to your extensions section, you’ll notice that the Recaptcha control is there and available for editing. You can ( and probably should ) create your own account on Recaptcha.net and put in a Public and Private key that corresponds to your site. The keys in there are for my site, but they are “global” keys which should work from any domain. You can also disable the control altogether, change it so it does not appear for logged in users, and change the theme.
The control also allows you to create a custom theme. To do so, you need to specify the name of the theme directly in the tag in the CommentView.ascx file mentioned above. So, for example:
<blog:RecaptchaControl ID="recaptcha" runat="server" TabIndex="8" Theme=”MyCustomTheme” />
You’ll find the rest of the info about what you need to do to skin the Recaptcha on their site.
As always, let me know if you’d like to see anything added to the control, or if you experience any bugs with it. I’ll be looking into the bug mentioned above this weekend to determine if the control is actually causing this issue mentioned above. I have a feeling I know how to fix it even if it isn’t, but I’ll just have to run some tests…