stefmde / EasyGCaptchaMVC

Easy ReCaptcha wrapper for MVC
MIT License
3 stars 1 forks source link
easy-to-use google google-recaptcha html-extension mvc nuget

EasyGCaptchaMVC

Easy Google ReCaptcha wrapper for MVC

My try to make it easy to implement the google ReCaptcha service for a MVC Website.


Steps to implement

  1. Get the api key pair from google: https://www.google.com/recaptcha
  2. Check the sample/demo project on GitHub
  3. Download and install the Package from nuget
  4. Implementing the HTML-Extension EasyGCaptchaGenerateCaptcha in the view
  5. Implementing the ActionFilterAttribute above the Action
  6. Check the given EasyGCaptchaResult in the Action

Quickstart

Html-Extension

Calling the Html-Extension EasyGCaptchaGenerateCaptcha and pass your publickey/websitekey to it is all you have to do in the view. You kann pass a few more settings like the Theme mode to it.

@Html.EasyGCaptchaGenerateCaptcha(publicKey: "xyzxyzxyz...xyzxyzxyz", theme: Theme.Dark)

ActionFilterAttribute and the result

Place the EasyGCaptcha-Attribute above your Post-Action and pass the privatekey to it. If you have done this you have to add the EasyGCaptchaResult easyGCaptchaResult-Parameter to your Action. In this parameter you can check the results from Google.

[HttpPost]
[EasyGCaptcha(PrivateKey ="xyzxyzxyz...xyzxyzxyz")]
public ActionResult Index(IndexViewModel model, EasyGCaptchaResult easyGCaptchaResult)
{
    if (easyGCaptchaResult.Success)
    {
        // Do your work here
    }
    model.EasyGCaptchaResult = easyGCaptchaResult;
    return View(model);
}

Some notes

Supported parameters

Extension

Attribute

Web.config

If you want to, you can add those two keys in your Web.config. If you do that, there is no more need to pass those keys to the extension or the Attribute:

<add key="EasyGCaptchaMVC.PublicKey" value="" />
<add key="EasyGCaptchaMVC.PrivateKey" value="" />