ASP.NET MVC 5 widget for google's recaptcha
-
Register in google's recaptcha 2 and get site key and secret.
-
Add site key and secret into web.config:
<configuration>
<appSettings>
<add key="Recaptcha2SiteKey" value="{Put here site key}"/>
<add key="Recaptcha2Secret" value="{Put here secret}"/>
...
-
Add Recaptcha2 class to your ASP.NET MVC 5 project.
-
Add Recaptcha2 to your forms view:
@using Recaptcha2
...
@using (Html.BeginForm())
{
...
<div class="form-group">
@Html.Label("Code", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Recaptcha2()
@Html.ValidationMessage("g-recaptcha-response", "", new { @class = "text-danger" })
</div>
</div>
*No need to create a property in model for captcha code.
- Add ValidateRecaptcha2 attribute to your controller's post action:
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateRecaptcha2(ErrorMessage = @"Recaptcha is required")]
Have fun.