本篇内容介绍了“.net如何实现滑动验证码”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!CaptchaData.csCaptchaData.
本篇内容介绍了“.net如何实现滑动验证码”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
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:验证码验证数据实体
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:证码图片信息实体
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 :资源实体类
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:滑动验证码实体类
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:滑动轨迹·
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:模板类·
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 :滑动数据实体类
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文件夹下。
“.NET如何实现滑动验证码”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
--结束END--
本文标题: .NET如何实现滑动验证码
本文链接: https://lsjlt.com/news/346442.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0