Async Recaptcha for BlogEngine.NET 1.6

by filip 20. February 2010 05:03

UPDATE: The latest installation instructions and files are available by following this link: http://www.bloodforge.com/post/BlogengineNET-reCaptcha-093-Installation-Instructions.aspx

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:

  1. Easy to implement – as any control, it needs to be pretty trivial to add it to an existing blog instance
  2. 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
  3. 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=1.6.0.1’. 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:

  1. Download Recaptcha.zip, extract the file in it, and place the file in the /App_Code/Controls folder.
  2. 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 ).
  3. 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.netand 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…

Tags: , , , , , ,

Web Development

Comments (10) -

The Lunatic
The Lunatic United States
2/20/2010 7:05:33 AM #

Thanks Filip - I got to your page via the blog engine forum.  I will try this solution asap, as I am getting killed by all the spam comments.

Reply

Filip
Filip United States
2/21/2010 12:19:15 AM #

Cool, I hope it works as expected!

Also, I made an update the the code above, since the 0.90 version linked to in this post has a potential vulnerability - it is possible to add a comment w/o passing the Recaptcha check by manually executing JavaScript on the page.  The latest version should resolve this issue.  However, this comes at a price, since it is now a little more difficult to integrate the control in the page.

If you do decide on upgrading to the latest version, keep in mind that you will need to restore the javascript being called in the submit button to the original javascript call.

The latest installation instructions are available at: www.bloodforge.com/.../...lation-Instructions.aspx

Reply

Abe
Abe United States
3/1/2010 11:44:59 PM #

Test Comment! Hope this works.  I've been having a hell of a time getting reCAPTCHA integrated with my BE.NET

Reply

Web Timesheet Blog
Web Timesheet Blog United States
3/6/2010 10:43:17 AM #

testing to see if it works before putting in my site.

Reply

psivarad
psivarad United States
5/25/2010 5:11:49 PM #

Thanks Filip.  Testing to see if it works before putting in my site.

Reply

Sam
Sam United Kingdom
6/23/2010 3:45:13 AM #

I've found this is the best way to prevent SPAM, my SPAM fell by about 60%! I still get some but it's far more manageable

Reply

lloyd
lloyd United States
9/23/2010 12:14:31 AM #

Make It do the job!

Reply

kamagra
kamagra United States
10/13/2010 4:20:16 PM #

Test Comment! Hope this works.  I've been having a hell of a time getting reCAPTCHA integrated with my BE.NET

Reply

kamagra
kamagra United States
10/14/2010 6:07:24 PM #

Thanks Filip - I got to your page via the blog engine forum.  I will try this solution asap, as I am getting killed by all the spam comments.

Reply

jeff
jeff United States
2/8/2011 1:00:57 AM #

Thanks Filip seems to work fine

Reply

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


About Filip Stanek

Death Note Pic I'm a developer at ACG in Cincinnati, OH. I like ASP.NET, Flash, and other web technologies, & enjoy playing chess, video games, etc.

Currently playing:
- StarCraft IIE-mail me Send mail

RecentComments

Comment RSS

Month List