An efficient and secured alternative of Google reCAPTCHA for ASP.NET 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.
-
Install it from
Nuget
:Install-Package EasyCaptchaCore -Version 1.0.1
-
Add
ICaptchaService
to yourStatrup
like this:services.AddTransient<ICaptchaService, CaptchaService>();
-
Enable
Session
inConfigure
method like this:app.UseSession();
-
Enable session
IdleTimeout
in yourConfigureServices
. 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); });
-
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 length of the captcha by the code below. (Default value is 5):
<img src="~/Captcha?l=6" alt="Captcha" />
-
You can change the background color of the captcha by the code below either by writting the color name or the keyword
random
. (Default istransparent
):<img src="~/Captcha?bc=black" alt="Captcha" />
<img src="~/Captcha?bc=random" alt="Captcha" />
-
You can change the forecolor of the captcha by the code below either by writting the color name or the keyword
random
. (Default israndom
):<img src="~/Captcha?fc=green" alt="Captcha" />
-
You can change type of the characters.
num
for numeric characters ormix
for alphanumeric characters. (Default ismix
):<img src="~/Captcha?t=num" alt="Captcha" />