ReCaptcha with Asp.Net MVC
Saturday, November 13th, 2010 | .net development, .Net Technology, .net web development, .net website development, Asp.Net, asp.net developers, aspdotnetstorefront solutions, b2c web development, dotnetnuke development, ecommerce solution | admin
These are just a few steps, as the latest reCAPTCHA library has all the pieces you need for asp.net MVC:
Step 1 – Create Your ReCAPTCHA Account
Go to the ReCAPTCHA site and sign up for your keys. You will get a private key and and a public key.
Step 2 – Download the .Net Library for ReCAPTCHA
Get reCAPTCHA Library and add this to your ASP.NET MVC project as a reference.
Step 3 – Create Action Filter and handle the Captcha validation
public class CaptchaValidator : ActionFilterAttribute
{
private const string challenge_field_key = "recaptcha_challenge_field";
private const string response_field_key = "recaptcha_response_field";
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var captchaCha_Value = filterContext.HttpContext.Request.Form[challenge_field_key];
var captchaResp_Value = filterContext.HttpContext.Request.Form[response_field_key];
var captchaValidtor = new Recaptcha.RecaptchaValidator
{
PrivateKey = '< Your Private Key>',
RemoteIP = filterContext.HttpContext.Request.UserHostAddress,
Challenge = captchaChallengeValue,
Response = captchaResponseValue
};
var captchaResponse = captchaValidtor.Validate();
filterContext.ActionParameters["captchaValid"] = captchaResponse.IsValid;
base.OnActionExecuting(filterContext);
}
}
[CaptchaValidator]
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult CreateComment( Int32 id, bool captchaValid )
{
.. ..... ...
}
Step 3 – Create a Html Helper to build and render the Captcha control
public static string GenerateReCaptcha( 'HtmlHelper helper' )
{
var captchaControl = new Recaptcha.RecaptchaControl
{
ID = "recaptcha",
Theme = "blackglass",
PublicKey = --<Your Public Key > --,
PrivateKey = --<Your Private Key > --
};
var htmlWriter = new HtmlTextWriter( new StringWriter() );
captchaControl.RenderControl(htmlWriter);
return htmlWriter.InnerWriter.ToString();
}
Step 4 Render the Captcha control
<:%= Html.GenerateReCaptcha() %>:
You can get ReCAPTCHA into your site in about 5 minutes, but it might take some time to perfect how you want it to integrate with you current scheme
Comments
No comments yet.