Skip to content

Bypassing the Fallback CAPTCHA

Jérémie Bertrand edited this page Mar 21, 2014 · 4 revisions

There are times you want to bypass the CAPTCHA fallback, for instance on your local development machine or when running automated tests. PoliteCaptcha includes a "fake" implementation of the ICaptchaGenerator and ICaptchaValidator interfaces that will bypass CAPTCHA generation and validation (BypassCaptchaGenerator and BypassCaptchaValidator, respectively).

To use them, you'll need to write some code that selectively chooses the implementation to return from ASP.NET MVC's dependency resolver (for instance, maybe you want to bypass for all local requests, or when a certain app setting is configured).

Here's an example that bypasses CAPTCHA for all local requests when using the Ninject dependency injection library:

Bind<ICaptchaGenerator>().ToMethod(context =>
{
    if (HttpContext.Current.Request.IsLocal)
        return new BypassCaptchaGenerator();
    else
        return new ReCaptchaGenerator();
});

Bind<ICaptchaValidator>().ToMethod(context =>
{
    if (HttpContext.Current.Request.IsLocal)
        return new BypassCaptchaValidator();
    else
        return new ReCaptchaValidator();
});