首页 > 互联资讯 > 技术交流  > 

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

目录
  • CaptchaData.cs
  • CaptchaValidateData.cs
  • ImageCaptchaInfo.cs
  • Resource.cs
  • SliderImageCaptchaInfo.cs
  • SlideTrack.cs
  • TemplatePair.cs
  • Track.cs

本节创建的类全部在工程的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文件夹下,这个后面的代码会进行说明。

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

到此这篇关于.NET 6实现滑动验证码的示例详解的文章就介绍到这了,更多相关.NET滑动验证码内容请搜索讯客以前的文章或继续浏览下面的相关文章希望大家以后多多支持讯客!

.NET 6实现滑动验证码的示例详解由讯客互联技术交流栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“.NET 6实现滑动验证码的示例详解