use. A graphic verification code function implemented by net core

Time:2022-4-27

Simplecaptcha is a simple to use, based on Net standard 2.0 graphic verification code module. It was inspired by EDI Wang’s article https://edi.wang/post/2018/10/13/generate-captcha-code-aspnet-core , I extracted the code that generated the verification code and encapsulated it to get this module. Here’s how to use it.

Basic usage

Install simplecaptcha

Search nuget for installing simplecaptcha

Install cache module

Simplecaptcha relies on Microsoft Extensions. Caching. The abstractions module is used to store the verification code, so you need to install the corresponding implementation package according to your needs in the project. For example, I use it hereMicrosoft.Extensions.Caching.Memory

Startup

Modify startup CS file is injected into the corresponding service:


services.AddMemoryCache()
    .AddSimpleCaptcha(builder =>
    {
        builder.UseMemoryStore();
    });

Injection interface

Inject the core interface icaptcha into the controller


private readonly ICaptcha _captcha;

public HomeController(ICaptcha captcha)
{
    _captcha = captcha;
}

Generate verification code

Using the icaptcha interfaceGenerateMethod to generate verification code


public IActionResult Captcha(string id)
{
    var info = _captcha.Generate(id);
    var stream = new MemoryStream(info.CaptchaByteData);
    return File(stream, "image/png");
}

verification

Using the icaptcha interfaceValidateMethod to verify the user’s submission


public IActionResult Validate(string id, string code)
{
    var result = _captcha.Validate(id, code);
    return Json(new { success = result });
}

A complete example can be found here:https://github.com/1992w/SimpleCaptcha/tree/master/src/SimpleCaptcha.Demo

to configure

Simplecaptcha reserves some default configuration items that you can modify as needed.

Set verification code length


services.AddSimpleCaptcha(builder =>
{
    builder.AddConfiguration(options =>
    {
        options.CodeLength = 6;
    });
});

Set picture size


services.AddSimpleCaptcha(builder =>
{
    builder.AddConfiguration(options =>
    {
        options.ImageWidth = 100;
        options.ImageHeight = 36;
    });
});

Set case sensitivity

By default, validation is not case sensitive


services.AddSimpleCaptcha(builder =>
{
    builder.AddConfiguration(options =>
    {
        options.IgnoreCase = false;
    });
});

Set the validity period of verification code

The default validity period of verification code is 5 minutes


services.AddSimpleCaptcha(builder =>
{
    builder.AddConfiguration(options =>
    {
        options.ExpiryTime =TimeSpan.FromMinutes(10);
    });
});

Set character set

Simplecaptcha providesICaptchaCodeGeneratorInterface is used to generate characters. The default implementation is from the character set012346789ABCDEFGHIJKLMNOPQRSTUVWXYZYou can inherit the icaptchacodegenerator interface to implement your own requirements.


public class MyCaptchaCodeGenerator : ICaptchaCodeGenerator
{
    public string Generate(int length)
    {
        throw new NotImplementedException();
    }
}

Configure your own generator


services.AddSimpleCaptcha(builder =>
{
    builder.AddConfiguration(options =>
    {
        options.CodeGenerator = new MyCaptchaCodeGenerator();
    });
});

Set personalized pictures

If the default generated image doesn’t meet your requirements, you can implement itICaptchaImageGeneratorModify the interface


public class CaptchaImageGenerator : ICaptchaImageGenerator
{
    public byte[] Generate(int width, int height, string captchaCode)
    {
        throw new NotImplementedException();
    }
}


services.AddSimpleCaptcha(builder =>
{
    builder.AddConfiguration(options =>
    {
        options.ImageGenerator = new CaptchaImageGenerator();
    });
});

source code

All source codes can be obtained here:https://github.com/1992w/SimpleCaptcha

This is about using Net # core implementation of a graphic verification code article is introduced here, more related For the content of net core graphic verification code, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you can support developeppaer in the future!