dehghani-mehdi / Easy-Captcha

An efficient and secured alternative of Google reCAPTCHA for .NET Core

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Easy Captcha

An efficient and secured alternative of Google reCAPTCHA for ASP.NET Core and ASP.NET MVC which has been written with C#. If you are being tired of struggling with Google reCAPTCHA and also worried about performance and banning issue with it, just take a minute to go through it for your consideration. I am certain that you won't be regret.

Easy Captcha

How to use

  • This project uses Bitmap to create a temporary image of random characters. In order to do that, you need to add System.Drawing.Common to your project. Use the command below to install it from Nuget:

    Install-Package System.Drawing.Common
    
  • Enable session in your ConfigureServices If you are using .NET Core or MVC. You can change the session timeout if you want. It uses to store the Captcha in the user session securely.

    services.AddSession(options =>
              {
                  options.IdleTimeout = TimeSpan.FromMinutes(10);
              });
  • Copy CaptchaController.cs to your project.

  • Use the code below in your View Layout:

    <img src="~/Captcha" alt="Captcha" />
  • In order to randomly change the Captcha text by clicking on it, you can use the code below:

    <img src="~/Captcha" alt="Captcha" style="cursor: pointer;" onclick="refreshImage(this);" />
    <script type="text/javascript">
              refreshImage = function (img) {
                  var source = img.src;
                  img.src = '';
    
                  source = source.split('?')[0];
                  source = source + '?' + new Date().getTime();
    
                  img.src = source;
              }
    </script>
  • Use the code below to compare the real Captcha with the user input:

    [HttpPost]
    public ActionResult Index(CaptchaModel model)
    {
        var realCaptcha = HttpContext.Session.GetString("captcha").ToLower();
        if (realCaptcha != model.Captcha)
            model.Message = "Ops...Wrong captcha!";
        else
            model.Message = "Congrats! Captcha has been matched!";
        return View(model);
    }
  • You can change the Captcha length, forecolor, background color, add more noisy line to make it more complicated, and so on so forth very easily in CaptchaController.cs.

About

An efficient and secured alternative of Google reCAPTCHA for .NET Core


Languages

Language:C# 64.1%Language:HTML 26.5%Language:CSS 8.0%Language:JavaScript 1.3%