Edi.Captcha 3.19.1
Edi.Captcha.AspNetCore
The Captcha module used in my blog
Usage
0. Install from NuGet
NuGet Package Manager
Install-Package Edi.Captcha
or .NET CLI
dotnet add package Edi.Captcha
1. Register in DI
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(20);
options.Cookie.HttpOnly = true;
});
services.AddSessionBasedCaptcha();
// Don't forget to add this line in your `Configure` method.
app.UseSession();
or you can customize the options
services.AddSessionBasedCaptcha(option =>
{
option.Letters = "2346789ABCDEFGHJKLMNPRTUVWXYZ";
option.SessionName = "CaptchaCode";
option.CodeLength = 4;
});
2. Generate Image
Using MVC Controller
private readonly ISessionBasedCaptcha _captcha;
public SomeController(ISessionBasedCaptcha captcha)
{
_captcha = captcha;
}
[Route("get-captcha-image")]
public IActionResult GetCaptchaImage()
{
var s = _captcha.GenerateCaptchaImageFileStream(
HttpContext.Session,
100,
36
);
return s;
}
Using Middleware
app.UseSession().UseCaptchaImage(options =>
{
options.RequestPath = "/captcha-image";
options.ImageHeight = 36;
options.ImageWidth = 100;
});
3. Add CaptchaCode Property to Model
[Required]
[StringLength(4)]
public string CaptchaCode { get; set; }
5. View
<div class="col">
<div class="input-group">
<div class="input-group-prepend">
<img id="img-captcha" src="~/captcha-image" />
</div>
<input type="text"
asp-for="CommentPostModel.CaptchaCode"
class="form-control"
placeholder="Captcha Code"
autocomplete="off"
minlength="4"
maxlength="4" />
</div>
<span asp-validation-for="CommentPostModel.CaptchaCode" class="text-danger"></span>
</div>
6. Validate Input
_captcha.ValidateCaptchaCode(model.CommentPostModel.CaptchaCode, HttpContext.Session)
To make your code look more cool, you can also write an Action Filter like this:
public class ValidateCaptcha : ActionFilterAttribute
{
private readonly ISessionBasedCaptcha _captcha;
public ValidateCaptcha(ISessionBasedCaptcha captcha)
{
_captcha = captcha;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var captchaedModel =
context.ActionArguments.Where(p => p.Value is ICaptchable)
.Select(x => x.Value as ICaptchable)
.FirstOrDefault();
if (null == captchaedModel)
{
context.ModelState.AddModelError(nameof(captchaedModel.CaptchaCode), "Captcha Code is required");
context.Result = new BadRequestObjectResult(context.ModelState);
}
else
{
if (!_captcha.Validate(captchaedModel.CaptchaCode, context.HttpContext.Session))
{
context.ModelState.AddModelError(nameof(captchaedModel.CaptchaCode), "Wrong Captcha Code");
context.Result = new ConflictObjectResult(context.ModelState);
}
else
{
base.OnActionExecuting(context);
}
}
}
}
and then
services.AddScoped<ValidateCaptcha>();
and then
public class YourModelWithCaptchaCode : ICaptchable
{
public string YourProperty { get; set; }
[Required]
[StringLength(4)]
public string CaptchaCode { get; set; }
}
[ServiceFilter(typeof(ValidateCaptcha))]
public async Task<IActionResult> SomeAction(YourModelWithCaptchaCode model)
{
// ....
}
Refer to https://edi.wang/post/2018/10/13/generate-captcha-code-aspnet-core
Showing the top 20 packages that depend on Edi.Captcha.
Packages | Downloads |
---|---|
MoongladePure.Comments
Package Description
|
9 |
MoongladePure.Comments
Package Description
|
7 |
MoongladePure.Comments
Package Description
|
6 |
MoongladePure.Comments
Package Description
|
5 |
Version | Downloads | Last updated |
---|---|---|
3.25.0 | 11 | 11/13/2024 |
3.24.0 | 96 | 08/12/2024 |
3.23.1 | 65 | 07/14/2024 |
3.23.0 | 14 | 07/09/2024 |
3.22.0 | 81 | 05/19/2024 |
3.21.2 | 106 | 03/26/2024 |
3.21.1 | 120 | 03/25/2024 |
3.21.0 | 6 | 04/28/2024 |
3.20.0 | 4 | 04/28/2024 |
3.19.1 | 5 | 04/28/2024 |
3.19.0 | 5 | 04/28/2024 |
3.18.0 | 4 | 04/28/2024 |
3.17.0 | 5 | 04/28/2024 |
3.16.0 | 5 | 04/28/2024 |
3.15.0 | 5 | 04/28/2024 |
3.14.0 | 4 | 04/28/2024 |
3.13.1 | 6 | 04/20/2024 |
3.13.0 | 5 | 04/28/2024 |
3.12.0 | 4 | 04/28/2024 |
3.11.0 | 4 | 04/28/2024 |
3.10.0 | 6 | 04/29/2024 |
3.9.0 | 4 | 04/28/2024 |
3.8.0 | 5 | 04/28/2024 |
3.7.0 | 5 | 04/28/2024 |
3.6.1 | 4 | 04/28/2024 |
3.6.0 | 4 | 04/28/2024 |
3.5.0 | 5 | 04/28/2024 |
3.4.0 | 4 | 04/28/2024 |
3.3.0 | 4 | 04/28/2024 |
3.2.0 | 5 | 04/28/2024 |
3.1.0 | 4 | 04/28/2024 |
3.0.1 | 6 | 04/28/2024 |
3.0.0 | 4 | 04/28/2024 |
2.2.0 | 4 | 04/28/2024 |
2.1.0 | 4 | 04/28/2024 |
2.0.0 | 5 | 04/28/2024 |
2.0.0-preview3 | 5 | 04/29/2024 |
2.0.0-preview2 | 5 | 04/29/2024 |
2.0.0-preview | 5 | 04/29/2024 |
1.3.1 | 4 | 04/28/2024 |
1.3.0 | 4 | 04/28/2024 |
1.2.0 | 5 | 04/28/2024 |
1.1.0 | 4 | 04/28/2024 |
1.0.0 | 4 | 04/28/2024 |