题外话,有网友说,这玩意根本很容易破解,确实是这样。但验证码这东西,就跟锁子很类似,防君子不防小人。验证码的发明其实是社会文明的退步。因为它阻碍了真正的使用者,却无法阻挡别有用心的人。但又有什么办法呢?

上一篇文章添加了一些基本类,本篇内容主要目的是创建一些接口类

上一节内容:NET 6 实现滑动验证码(二)、基本数据


ValidateResult.cs

在项目根目录下,创建ValidateResult.cs。其功能是提供验证后返回的信息

namespace SlideCaptcha
{
    public class ValidateResult
    {
        public ValidateResultType Result { get; set; }
        public string Message { get; set; }

        public static ValidateResult Success()
        {
            return new ValidateResult { Result = ValidateResultType.Success, Message = "成功" };
        }

        public static ValidateResult Fail()
        {
            return new ValidateResult { Result = ValidateResultType.ValidateFail, Message = "验证失败" };
        }

        public static ValidateResult Timeout()
        {
            return new ValidateResult { Result = ValidateResultType.Timeout, Message = "验证超时" };
        }

        public enum ValidateResultType
        {
            Success = 0,
            ValidateFail = 1,
            Timeout = 2
        }
    }
}

ICaptcha.cs

在Interface文件夹下,建一个ICaptcha.cs,用于生成验证码和进行校验。

using SlideCaptcha.Model;
using System.Threading.Tasks;

namespace SlideCaptcha.Interface
{
    public interface ICaptcha
    {
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="captchaId">验证码id</param>
        /// <returns></returns>
        Task<CaptchaData> GenerateCaptchaImageAsync(string type, string captchaId = null);

        /// <summary>
        /// 校验
        /// </summary>
        /// <param name="captchaId">验证码id</param>
        /// <param name="slideTrack">滑动轨迹</param>
        /// <returns></returns>
        ValidateResult Validate(string captchaId, SlideTrack slideTrack);
    }
}

IResourceHandler.cs

IResourceHandler.cs 用于生成验证码资源(验证码模板)。

using SlideCaptcha.Model;
using System.Threading.Tasks;

namespace SlideCaptcha.Interface
{
    public interface IResourceHandler
    {
        /// <summary>
        /// 判断传入的资源类型是否存在
        /// </summary>
        /// <param name="handlerType"></param>
        /// <returns></returns>
        bool CanHandle(string handlerType);

        /// <summary>
        /// 生成资源
        /// </summary>
        /// <param name="resource"></param>
        /// <returns></returns>
        Task<byte[]> Handle(Resource resource);
    }
}

IResourceHandlerManager.cs

IResourceHandlerManager.cs用于根据资源(验证码模板),获取资源byte[]数据。

using SlideCaptcha.Model;
using System.Threading.Tasks;

namespace SlideCaptcha.Interface
{
    public interface IResourceHandlerManager
    {
        /// <summary>
        /// 根据资源,生成所需要的byte[]
        /// </summary>
        /// <param name="resource"></param>
        /// <returns></returns>
        Task<byte[]> Handle(Resource resource);
    }
}

IResourceManager.cs

IResourceManager.cs用于获取验证码背景数据与验证码模板数据

using System.Threading.Tasks;

namespace SlideCaptcha.Interface
{
    public interface IResourceManager
    {
        /// <summary>
        /// 随机获取验证码图片背景
        /// </summary>
        /// <returns></returns>
       Task<byte[]> RandomBackground();
       /// <summary>
       /// 随机获取模板
       /// </summary>
       /// <param name="type">模板类型,滑动、旋转、点选</param>
       /// <returns></returns>
       Task<(byte[], byte[])> RandomTemplate(string type);
    }
}

IResourceProvider.cs

IResourceProvider.cs用于提供验证码背景图片与模板图片的List

using SlideCaptcha.Model;
using System.Collections.Generic;

namespace SlideCaptcha.Interface
{
    public interface IResourceProvider
    {
        /// <summary>
        /// 提供验证码背景图片
        /// </summary>
        /// <returns></returns>
        List<Resource> Backgrounds();

        /// <summary>
        /// 提供模板资源
        /// </summary>
        /// <returns></returns>
        List<TemplatePair> Templates();
    }
}

ISliderCaptchaImageGenerator.cs

ISliderCaptchaImageGenerator.cs用于生成滑动验证码

using SlideCaptcha.Model;
using System.Threading.Tasks;

namespace SlideCaptcha.Interface
{
    public interface ISliderCaptchaImageGenerator
    {
        /// <summary>
        /// 生成滑动验证码
        /// </summary>
        /// <param name="captchaId">验证码ID</param>
        /// <returns></returns>
        Task<SliderImageCaptchaInfo> Generate(string captchaId);
    }
}

IStorage.cs

IStorage.cs 用于缓存的操作

using System;

namespace SlideCaptcha.Interface
{
    public interface IStorage
    {
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="absoluteExpiration"></param>
        void Set<T>(string key, T value, DateTimeOffset absoluteExpiration);

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        T Get<T>(string key);

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key"></param>

        void Remove(string key);
    }
}

IValidator.cs

IValidator.cs 用于提供校验验证码数据。

using SlideCaptcha.Model;

namespace SlideCaptcha.Interface
{
    public interface IValidator
    {
        /// <summary>
        /// 校验验证码数据
        /// </summary>
        /// <param name="slideTrack">滑动</param>
        /// <param name="captchaValidateData">校验</param>
        /// <returns></returns>
        bool Validate(SlideTrack slideTrack, CaptchaValidateData captchaValidateData);
    }
}

下载方式:
扫描公众号二维码,或搜索公众号fenxiang3389关注我,回复captcha下载,压缩包包含了验证码类库、服务端API、HTML+JQuery完整代码、vue3组件代码及演示代码!

欢迎大家关注我的微信公众号,一起进步,一起成长
posted on 2022-11-29 10:55  gmval  阅读(1468)  评论(3编辑  收藏  举报