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

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

时间:2022-11-28 11:36:57浏览次数:69  
标签:set string get 验证码 cs 滑动 NET public

上一节,介绍了滑动验证码的基本情况,也创建了一个.NET 6的工程。本节内容是创建一些基础类。


目录


本节创建的类全部在工程的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/19mx24FXrnqz9u2mmFqlr6g?pwd=7636

标签:set,string,get,验证码,cs,滑动,NET,public
From: https://www.cnblogs.com/gmval/p/16931721.html

相关文章

  • .Net 6 配置日志
    前言  .Net6与之前的配置有点不一样了记录下日志配置方式。当前日志以Serilog为例,.Net6的日志由内置的Logger获取,然后可以交给Serilog|NLog等框架处理,框架通过他......
  • SQL serve 安装报错,缺少.net3.5组件
    今天安装sqlserver2014,遇到服务器没有安装.net3.5,的报错,上网搜索一番,找到一篇博文(非常详细,拷贝进来以备后用)操作之后略有区别,为表尊重原文地址https://www.cnblogs.co......
  • Use Kubekey to quickly deploy Kubernetes
    IntroductionStartwritingdeploymentprofilesStartthedeploymentDeploymentcomplete,checkpodstatusIntroductionKubekeyisanopensourceKubernetes......
  • 长度最小子数组-LeetCode209 滑动窗口
    力扣:https://leetcode.cn/problems/minimum-size-subarray-sum/题目  给定一个含有 n 个正整数的数组和一个正整数target。找出该数组中满足其和≥target的长......
  • .NET 生成可跨平台得图片二维码
    下载包nugetSkiaSharp创建帮助类VerificationCodeHelper点击查看代码publicclassVerifyCodeHelper{///<summary>///获取图像数字验证码......
  • 基于.NET三维控件的个性化管道软件开发
    1简介管道广泛用于化工、工厂、建筑、市政等方面,关系到国计民生。虽然管道设计软件种类繁多,有的也非常强大(然而也非常昂贵),但也并不能完全满足个性化需要。如何快速开发......
  • Modbus RTU转Profinet网关将信衡控制秤接入Profinet的配置过程
    本案例控制的主要对象是信衡控制秤仪表的数据参数的显示和报警,仪表型号XH3180-K,系统主PLC选用西门子S71200,将设备接入西门子PLC的采集数据模块采用MODBUS转PROFINE......
  • 拓端tecdat|r语言代码编写对LASSO,Ridge岭回归和Elastic Net模型实现
     介绍Glmnet是一个通过惩罚最大似然来拟合广义线性模型的包。正则化路径是针对正则化参数λ的值网格处的套索或弹性网络罚值计算的。该算法速度极快,可以利用输入矩阵中的稀......
  • Netty源码-00-启动
    一源码我fork的源码带注释分支study二环境版本Netty4.1.169Java8三Samples1服务端1.1启动类/**Copyright2012TheNettyProject**......
  • 拓端tecdat|python编程代写对网络图networkx进行社区检测和彩色绘图
    python对网络图networkx进行社区检测和彩色绘图 以下是创建图表,检测其中的社区,然后在少于10行的python中使用由其社区着色的节点进行可视化的方法......