.NET 6实现滑动验证码的示例详解

寻技术 ASP.NET编程 / 其他编程 2024年01月27日 131

本节创建的类全部在工程的Model目录下:

CaptchaData.cs

CaptchaData.cs:验证码的数据类实体

namespace SlideCaptcha.Model
{
    public class CaptchaData
    {
        /// <summary>
        /// id
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 背景图(含凹槽)
        /// </summary>
        public string BackgroundImage { get; set; }
        /// <summary>
        /// 滑动块图
        /// </summary>
        public string SliderImage { get; set; }

        public CaptchaData(string id, string backgroundImage, string sliderImage)
        {
            Id = id;
            BackgroundImage = backgroundImage;
            SliderImage = sliderImage;
        }
    }
}

id表示验证码的id值,用于校验验证码,BackgroundImage 是验证码图片base64,包含了凹槽。凹槽模板在templates目录下。SliderImage 是滑块图片。模板在templates下。

CaptchaValidateData.cs

CaptchaValidateData.cs:验证码验证数据实体

namespace SlideCaptcha.Model
{
    public class CaptchaValidateData
    {
        /// <summary>
        /// 滑动比例
        /// </summary>
        public float Percent { get; set; }

        /// <summary>
        /// 容错值(校验时用,缺口位置与实际滑动位置匹配容错范围)
        /// </summary>
        public float Tolerant { get; set; }

        public CaptchaValidateData(float percent, float tolerant)
        {
            Percent = percent;
            Tolerant = tolerant;
        }
    }
}

ImageCaptchaInfo.cs

ImageCaptchaInfo.cs:证码图片信息实体

namespace SlideCaptcha.Model
{
    public class ImageCaptchaInfo
    {
        /// <summary>
        /// 背景图宽
        /// </summary>
        public int BackgroundImageWidth { get; set; }
        /// <summary>
        /// 背景图高
        /// </summary>
        public int BackgroundImageHeight { get; set; }
        /// <summary>
        /// 背景图
        /// </summary>
        public string BackgroundImageBase64 { get; set; }
        /// <summary>
        /// 滑动块图宽
        /// </summary>
        public int SliderImageWidth { get; set; }
        /// <summary>
        /// 滑动块图高
        /// </summary>
        public int SliderImageHeight { get; set; }
        /// <summary>
        /// 滑动块图
        /// </summary>
        public string SliderImageBase64 { get; set; }
    }
}

验证码实体类的基类,滑动验证码、旋转验证码、点选验证码、拼接验证码都基于这个类。

Resource.cs

Resource.cs :资源实体类

namespace SlideCaptcha.Model
{
    public class Resource
    {
        public Resource(){}
        /// <summary>
        /// 资源数据
        /// </summary>
        public string Data { get; set; }

        /// <summary>
        /// 资源类型
        /// </summary>
        public string Type { get; set; }

        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">资源类型</param>
        /// <param name="data">资源数据</param>
        public Resource(string type,string data)
        {
            Type = type;
            Data = data;
        }
    }
}

资源数据,Data为要生成验证码图片路径。Type为类型,目前仅为本地文件,后期将扩展URL模式。

SliderImageCaptchaInfo.cs

SliderImageCaptchaInfo.cs:滑动验证码实体类

using SlideCaptcha.Exceptions;

namespace SlideCaptcha.Model
{
    public class SliderImageCaptchaInfo : ImageCaptchaInfo
    {
        /// <summary>
        /// 凹槽x坐标
        /// </summary>
        public int X { get; set; }
        /// <summary>
        /// 凹槽y坐标
        /// </summary>
        public int Y { get; set; }
        /// <summary>
        /// 凹槽位置百分比
        /// </summary>
        public float Percent
        {
            get
            {
                if (BackgroundImageWidth <= 0) return 0;
                return 1.0f * X / BackgroundImageWidth;
            }
        }

        public void Check()
        {
            // 校验
            if (this.X <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo数据异常: {nameof(X)}小于等于0");
            if (this.Y <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo数据异常: {nameof(Y)}小于等于0");
            if (this.BackgroundImageWidth <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo数据异常: {nameof(BackgroundImageWidth)}小于等于0");
            if (this.BackgroundImageHeight <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo数据异常: {nameof(BackgroundImageHeight)}小于等于0");
            if (this.SliderImageWidth <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo数据异常: {nameof(SliderImageWidth)}小于等于0");
            if (this.SliderImageHeight <= 0) throw new SlideCaptchaException($"SliderImageCaptchaInfo数据异常: {nameof(SliderImageHeight)}小于等于0");
            if (string.IsNullOrWhiteSpace(this.BackgroundImageBase64)) throw new SlideCaptchaException($"SliderImageCaptchaInfo数据异常: {nameof(BackgroundImageBase64)}为空");
            if (string.IsNullOrWhiteSpace(this.SliderImageBase64)) throw new SlideCaptchaException($"SliderImageCaptchaInfo数据异常: {nameof(SliderImageBase64)}为空");
        }
    }
}

SlideTrack.cs

·SlideTrack.cs:滑动轨迹·

using SlideCaptcha.Exceptions;
using System;
using System.Collections.Generic;

namespace SlideCaptcha.Model
{
    /// <summary>
    /// 滑动轨迹
    /// </summary>
    public class SlideTrack
    {
        /// <summary>
        /// 背景图片宽度(可能经过缩放,不是原始图片宽高)
        /// </summary>
        public int BackgroundImageWidth { get; set; }
        /// <summary>
        /// 背景图片高度(可能经过缩放,不是原始图片宽高)
        /// </summary>
        public int BackgroundImageHeight { get; set; }
        /// <summary>
        /// 滑块图片宽度(可能经过缩放,不是原始图片宽高)
        /// </summary>
        public int SliderImageWidth { get; set; }
        /// <summary>
        /// 滑块图片高度(可能经过缩放,不是原始图片宽高)
        /// </summary>
        public int SliderImageHeight { get; set; }
        /// <summary>
        /// 滑动开始时间(可能经过缩放,不是原始图片宽高)
        /// </summary>
        public DateTime StartTime { get; set; }
        /// <summary>
        /// 滑动结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
        /// <summary>
        /// 轨迹
        /// </summary>
        public List<Track> Tracks { get; set; }

        /// <summary>
        /// 滑动比例
        /// </summary>
        public float Percent
        {
            get
            {
                if (this.BackgroundImageWidth <= 0) return -1;
                if (this.Tracks.Count <= 0) return -1;

                var lastTrack = this.Tracks[this.Tracks.Count - 1];
                return 1.0f * lastTrack.X / this.BackgroundImageWidth;
            }
        }

        public void Check()
        {
            // 校验
            if (this.BackgroundImageWidth <= 0) throw new SlideCaptchaException($"SlideTrack数据异常: {nameof(BackgroundImageWidth)}小于等于0");
            if (this.BackgroundImageHeight <= 0) throw new SlideCaptchaException($"SlideTrack数据异常: {nameof(BackgroundImageHeight)}小于等于0");
            if (this.SliderImageWidth <= 0) throw new SlideCaptchaException($"SlideTrack数据异常: {nameof(SliderImageWidth)}小于等于0");
            if (this.SliderImageHeight <= 0) throw new SlideCaptchaException($"SlideTrack数据异常: {nameof(SliderImageHeight)}小于等于0");
            if (this.StartTime == DateTime.MinValue) throw new SlideCaptchaException($"SlideTrack数据异常: {nameof(StartTime)}为空");
            if (this.EndTime == DateTime.MinValue) throw new SlideCaptchaException($"SlideTrack数据异常: {nameof(EndTime)}为空");
        }

        public void CheckTracks()
        {
            if (this.Tracks == null || this.Tracks.Count == 0) throw new SlideCaptchaException($"SlideTrack数据异常: {nameof(Tracks)}为空");
        }
    }
}

TemplatePair.cs

·TemplatePair.cs:模板类·


namespace SlideCaptcha.Model
{
    public class TemplatePair
    {

        public TemplatePair() { }

        /// <summary>
        /// 滑块资源
        /// </summary>
        public Resource Slider { get; set; }

        /// <summary>
        /// 凹槽资源
        /// </summary>

        public Resource Notch { get; set; }

        /// <summary>
        /// 模板类型
        /// </summary>
        public string TYPE { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="slider">滑块资源</param>
        /// <param name="notch">凹槽资源</param>
        /// <param name="type">末班类型</param>
        public TemplatePair(Resource slider, Resource notch,string type)
        {
            Slider = slider;
            Notch = notch;
            TYPE = type;
        }
        /// <summary>
        /// 创建模板
        /// </summary>
        /// <param name="slider">滑块资源</param>
        /// <param name="notch">凹槽资源</param>
        /// <returns></returns>
        public static TemplatePair Create(Resource slider, Resource notch,string type)
        {
            return new TemplatePair(slider, notch,type);
        }
        
    }
}

Track.cs

Track.cs :滑动数据实体类

namespace SlideCaptcha.Model
{
    public class Track
    {
        /// <summary>
        /// X轴
        /// </summary>
        public int X { get; set; }

        /// <summary>
        /// Y轴
        /// </summary>
        public int Y { get; set; }

        /// <summary>
        /// 时间
        /// </summary>
        public int T { get; set; }
    }
}

代码中SlideCaptchaException是自定义错误类。自定义错误类在Extensions文件夹下,这个后面的代码会进行说明。

下载地址:https://pan.baidu.com/s/1YNlcO0llzO1041XqE4IYNg 提取码:2vf5

关闭

用微信“扫一扫”