+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Server-Side CAPTCHA for Sitefinity Forms

by Stanislav Velikov
We have had numerous requests of implementing RadCaptcha into a web form. This short post illustrates how to modify a Sitefinity form control to perform a server-side validation.

Sitefinity 6.2 release will add captcha for form controls out of the box! 

Register CAPTCHA as Form Control

First, go to the Sitefinity backend in Administration >> Settings switch to Advanced Settings and browse Toolboxes >> FormControls >> Sections >> Common >> Tools (this will register a RadCaptcha control inside Common section)

Click Create new and set Control CLR Type or Virtual Path to Telerik.Web.UI.RadCaptcha, Telerik.Web.UI put a name (without spaces) and a title for the UI.

Adding Custom Form Library to Sitefinity project

Next step is to implement new form control to fire RadCaptcha validation.

In Visual Studio add a project library. Add all necessary .dll dependencies (Telerik.Sitefinity.dll, Telerik.Web.UI.dll) found in SitefinityWebApp\bin. Add this library as a reference in SitefinityWebApp in order to be accessible in Sitefinity.

Code:

using System;
using System.ComponentModel;
using System.Linq;
using Telerik.Sitefinity.Modules.Forms.Web.UI;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web.UI;
using Telerik.Web.UI;
 
namespace NewFormControl
{
    public class FormControlWithCaptcha:FormsControl
    {
        private readonly string captchaErrorMessage = "Invalid Code, please try again";
 
        private RadCaptcha captcha;
 
        protected RadCaptcha MyCaptcha
        {
            get
            {
                if (!SystemManager.IsDesignMode)
                {
                    //retrieve the captcha control from the list of controls in the form
                    foreach (object control in base.FormControls.Controls[1].Controls)
                    {
                        if (control.GetType() != typeof(RadCaptcha))
                        {
                            continue;
                        }
                        this.captcha = control as RadCaptcha;
                        break;
                    }
                }
                return this.captcha;
            }
        }
 
        protected override void InitializeControls(GenericContainer container)
        {
            if (!SystemManager.IsDesignMode)
            {
                base.InitializeControls(container);
                base.BeforeFormSave += new EventHandler<CancelEventArgs>(this.FormsControlCustom_BeforeFormSave);
            }
        }
 
        protected void FormsControlCustom_BeforeFormSave(object sender, CancelEventArgs e)
        {
            if (this.MyCaptcha != null && !this.MyCaptcha.IsValid)
            {
                e.Cancel = true;
                this.MyCaptcha.ErrorMessage = this.captchaErrorMessage;
            }
        }
    }
}

The code is quite simple – it inherits old FormsControl, adds an event to fire validation in base.InitializeControls(container) method. Adds a captcha error message, shown if the validation code doesn`t match.
 

The referenced project must be built under .NET 4.0 (if the target framework is 4.5 it will be incompatible with SitefinityWebApp)

Registering the Custom Form as a page control

Register this new form control as a page control.

Go to Administration >> Settings switch to Advanced Settings and browse Toolboxes >> PageControls >> Sections >> ContentToolboxSection >> Tools

Click Create new and set Control CLR Type or Virtual Path to NewFormControl.FormControlWithCaptcha put a name (without spaces) and a title for the UI.



As a result we got fully working form control with captcha:



Here you can download the complete sample.

5 comments

Leave a comment
  1. Steve Sep 23, 2013
    Well this seems incredibly familiar ;)

    http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity-/how-to-add-captcha-in-custom-sitefinity-form-builder-
  2. Markus Sep 25, 2013
    And me personally, still think captcha should simply be something a end user can tick a checkbox: Use Captcha. That would be a real solution. Blogpost are nice but if you have 10 sites and more you got to remember every post and do it again and again

    Markus
  3. Markus Sep 25, 2013
    And me personally, still think captcha should simply be something a end user can tick a checkbox: Use Captcha. That would be a real solution. Blogpost are nice but if you have 10 sites and more you got to remember every post and do it again and again

    Markus
  4. Rory Jarrard Oct 10, 2013
    Having trouble with the fact that when you try to drop this on a page, it complains that it doesn't extend System.Web.UI.UserControl, not FormsControl.  The code is straightforward, so I'm assuming this is a problem with registering within Sitefinity backend.  Can you help with this?
  5. Chris Carrier Feb 06, 2014
    Hooray.  Another add on for something that should be in out of the box since 2005.  Upgrade to 6.2?  That's a very big deal for us.  Every time I look for a solution to something that should be already included or work properly SF it's a big complicated process and I have to abandon it or put it off.  I'm constantly having to apologize for this software to my company.  Now it's "Sorry you get 200 spam entires a day.  Fixing it is complicated, probably won't work, we'll have to redo all our forms and we don't have the capacity.  But the NEXT upgrade will solve all our problems."

    Sorry to gripe.

    Leave a comment