.net 6实现滑动验证码的示例详解-kb88凯时官网登录

来自:网络
时间:2024-06-10
阅读:

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

captchadata.cs

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

namespace slidecaptcha.model
{
    public class captchadata
    {
        /// 
        /// id
        /// 
        public string id { get; set; }
        /// 
        /// 背景图(含凹槽)
        /// 
        public string backgroundimage { get; set; }
        /// 
        /// 滑动块图
        /// 
        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
    {
        /// 
        /// 滑动比例
        /// 
        public float percent { get; set; }
        /// 
        /// 容错值(校验时用,缺口位置与实际滑动位置匹配容错范围)
        /// 
        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
    {
        /// 
        /// 背景图宽
        /// 
        public int backgroundimagewidth { get; set; }
        /// 
        /// 背景图高
        /// 
        public int backgroundimageheight { get; set; }
        /// 
        /// 背景图
        /// 
        public string backgroundimagebase64 { get; set; }
        /// 
        /// 滑动块图宽
        /// 
        public int sliderimagewidth { get; set; }
        /// 
        /// 滑动块图高
        /// 
        public int sliderimageheight { get; set; }
        /// 
        /// 滑动块图
        /// 
        public string sliderimagebase64 { get; set; }
    }
}

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

resource.cs

resource.cs :资源实体类

namespace slidecaptcha.model
{
    public class resource
    {
        public resource(){}
        /// 
        /// 资源数据
        /// 
        public string data { get; set; }
        /// 
        /// 资源类型
        /// 
        public string type { get; set; }
        
        /// 
        /// 构造函数
        /// 
        /// 资源类型
        /// 资源数据
        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
    {
        /// 
        /// 凹槽x坐标
        /// 
        public int x { get; set; }
        /// 
        /// 凹槽y坐标
        /// 
        public int y { get; set; }
        /// 
        /// 凹槽位置百分比
        /// 
        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
{
    /// 
    /// 滑动轨迹
    /// 
    public class slidetrack
    {
        /// 
        /// 背景图片宽度(可能经过缩放,不是原始图片宽高)
        /// 
        public int backgroundimagewidth { get; set; }
        /// 
        /// 背景图片高度(可能经过缩放,不是原始图片宽高)
        /// 
        public int backgroundimageheight { get; set; }
        /// 
        /// 滑块图片宽度(可能经过缩放,不是原始图片宽高)
        /// 
        public int sliderimagewidth { get; set; }
        /// 
        /// 滑块图片高度(可能经过缩放,不是原始图片宽高)
        /// 
        public int sliderimageheight { get; set; }
        /// 
        /// 滑动开始时间(可能经过缩放,不是原始图片宽高)
        /// 
        public datetime starttime { get; set; }
        /// 
        /// 滑动结束时间
        /// 
        public datetime endtime { get; set; }
        /// 
        /// 轨迹
        /// 
        public list tracks { get; set; }
        /// 
        /// 滑动比例
        /// 
        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() { }
        /// 
        /// 滑块资源
        /// 
        public resource slider { get; set; }
        /// 
        /// 凹槽资源
        /// 
        public resource notch { get; set; }
        /// 
        /// 模板类型
        /// 
        public string type { get; set; }
        /// 
        /// 构造函数
        /// 
        /// 滑块资源
        /// 凹槽资源
        /// 末班类型
        public templatepair(resource slider, resource notch,string type)
        {
            slider = slider;
            notch = notch;
            type = type;
        }
        /// 
        /// 创建模板
        /// 
        /// 滑块资源
        /// 凹槽资源
        /// 
        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
    {
        /// 
        /// x轴
        /// 
        public int x { get; set; }
        /// 
        /// y轴
        /// 
        public int y { get; set; }
        /// 
        /// 时间
        /// 
        public int t { get; set; }
    }
}

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

下载地址: 提取码:2vf5

返回顶部
顶部
网站地图