其他分享
首页 > 其他分享> > .NET 6 实现滑动验证码(二)、基本数据

.NET 6 实现滑动验证码(二)、基本数据

作者:互联网

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模式。

标签:验证码,数据类实,templates,构造函数
来源: