Using Recaptcha in the C# CreateUserWizard

By XeroCube | April 16, 2011

I have been playing around with a new .NET website for a while now. One of the great features of .NET is the concept of the CreateUserWizard. It helps take some of the tedium out of user registration and membership. However, out of the box, it is very basic.

In my solution, I wanted to ensure the user was indeed a real live human being. Outside of creating a largely robust Turing test, I decided to implement a captcha. While somewhat annoying, it can be highly effective in filtering a bunch of spam traffic. Add into the mix an email verification scheme, I should be set.

I chose Recaptcha as my control of choice. It appears to be widely used and officially supported. One problem, though, is you can’t just drop it in and have it work. I will assume you have the control correctly setup in your project. If not, repeat after me: “Google is my friend.” To use the control, find (or create) the appropriate WizardStep in your control and insert the control code. If you don’t know this, you will once you’ve registered for Recaptcha. If not, repeat after me: “Bing is also my friend.”

The second task was to have the CreateUserWizard actually verify the Recaptcha validation. This proved tricky, but not insurmountable. In fact, once I learned of the CreatingUser method (not to be confused with the CreatedUser method), it was straightforward. I have provided a sampling of code below (provided as-is with no warranty of quality… use at your own risk… I cannot be held liable for your code screwing up… blah blah blah):

protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
{
    // validate recaptcha
    Recaptcha.RecaptchaControl mycaptcha = (Recaptcha.RecaptchaControl)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("YOUR_RECAPTCHA_CONTROL_NAME");
    mycaptcha.Validate();
    Boolean answer = mycaptcha.IsValid;
    if (answer == false)
    {
        // captcha failed
        // cancel process
        e.Cancel = true;
    }
    else
    {
        // captcha succeeded, proceed and do nothing
    }
}

Hope this helps you out. Happy coding!

Topics: Code, Tech | 4 Comments »

4 Responses to “Using Recaptcha in the C# CreateUserWizard”

Aman Says:
August 27th, 2011 at 3:26 am

thx. awesum work…

Aman Says:
August 27th, 2011 at 4:09 am

awesum wrk…thx alot

Dave Says:
November 28th, 2012 at 3:25 pm

Thank you for your help. Great Article.

As You said, pay attention to the Creating vs Created

🙂

Brandon Says:
January 22nd, 2014 at 4:04 pm

What if you are using the SSL version of reCaptcha? With the SSL version you aren’t using an controls so FindControl doesn’t seem like it would be an option. Any advice? I’ve been searching and searching for a solution for this.

Comments

Archives

Blogroll

RockinMedia