Edi.Captcha 3.19.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Edi.Captcha --version 3.19.0                
NuGet\Install-Package Edi.Captcha -Version 3.19.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Edi.Captcha" Version="3.19.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Edi.Captcha --version 3.19.0                
#r "nuget: Edi.Captcha, 3.19.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Edi.Captcha as a Cake Addin
#addin nuget:?package=Edi.Captcha&version=3.19.0

// Install Edi.Captcha as a Cake Tool
#tool nuget:?package=Edi.Captcha&version=3.19.0                

Edi.Captcha.AspNetCore

The Captcha module used in my blog

.NET

NuGet

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

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Edi.Captcha:

Repository Stars
EdiWang/Moonglade
Blog system of https://edi.wang, runs on Microsoft Azure
AiursoftWeb/Infrastructures
Mirror of: https://gitlab.aiursoft.cn/aiursoft/infrastructures
Version Downloads Last updated
3.26.0 81 2/8/2025
3.25.0 878 11/13/2024
3.24.0 1,372 8/11/2024
3.23.1 555 7/14/2024
3.23.0 175 7/8/2024
3.22.0 969 5/19/2024
3.21.2 1,121 3/11/2024
3.21.1 1,141 1/29/2024
3.21.0 905 12/15/2023
3.20.0 314 12/11/2023
3.19.1 1,388 11/6/2023
3.19.0 540 10/11/2023
3.18.0 778 9/16/2023
3.17.0 574 8/29/2023
3.16.0 867 7/13/2023
3.15.0 4,137 1/5/2023
3.14.0 1,333 11/9/2022
3.13.1 3,839 9/14/2022
3.13.0 4,132 7/31/2022
3.12.0 410 7/31/2022
3.11.0 1,569 6/12/2022
3.10.0 2,586 2/9/2022
3.9.0 570 1/10/2022
3.8.0 272 1/9/2022
3.7.0 452 12/9/2021
3.6.1 871 12/8/2021
3.6.0 513 11/13/2021
3.5.0 498 11/9/2021
3.4.0 358 11/9/2021
3.3.0 2,047 6/7/2021
3.2.0 1,431 4/1/2021
3.1.0 430 3/31/2021
3.0.1 2,992 11/27/2020
3.0.0 658 11/11/2020
2.2.0 3,929 12/4/2019
2.1.0 641 11/22/2019
2.0.0 1,027 9/24/2019
2.0.0-preview3 294 9/17/2019
2.0.0-preview2 291 9/15/2019
2.0.0-preview 297 9/11/2019
1.3.1 1,432 5/1/2019
1.3.0 691 4/11/2019
1.2.0 889 1/30/2019
1.1.0 766 12/13/2018
1.0.0 1,441 11/11/2018