Usage Example

Rendering UI

// Razor view
// in Login.cshtml

@using ReCaptcha.Mvc5;
....
<div>
....
 @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod .Post, new { @class = "form-horizontal", role = "form" }))
....
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<!--Render the recaptcha-->
@Html.reCAPTCHA("public_key")
</div>
</div>
....
}
....

 

Verifying Captcha

...
using ReCaptcha.Mvc5;
...

// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
// Verify the recaptcha response.
ReCaptchaResponse response = await this.verifyReCAPTCHA(model, "private_key", true);
if (response.Success)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
else
{
ModelState.AddModelError("", "Stop spamming. Read books !");
}
}

// If we got this far, something failed, redisplay form
return View(model);
}

 

Sample model

public class LoginViewModel : ReCaptcha.Mvc5.Model.ReCaptchaViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }

[Required(ErrorMessage="Stop spammig. Read books !")]
public override string recaptcha_response_field { get; set; }
}

Last edited Feb 11, 2014 at 11:50 AM by rajdeepp, version 6