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.
-
This project uses
Bitmap
to create a temporary image of random characters. In order to do that, you need to addSystem.Drawing.Common
to your project. Use the command below to install it fromNuget
:Install-Package System.Drawing.Common
-
Enable session in your
ConfigureServices
If you are using.NET Core
orMVC
. You can change the session timeout if you want. It uses to store theCaptcha
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 inCaptchaController.cs
.