上一节添加了一些基本类,本节创建一些接口类
上一节内容:NET 6 实现滑动验证码(二)、基本数据
接口类(Interface文件夹下)
ValidateResult.cs
在项目根目录下,创建ValidateResult.cs。其功能是提供验证后返回的信息
namespace SlideCaptcha
{
public class ValidateResult
{
public ValidateResultType Result { get; set; }
public string Message { get; set; }
public static ValidateResult Success()
{
return new ValidateResult { Result = ValidateResultType.Success, Message = "成功" };
}
public static ValidateResult Fail()
{
return new ValidateResult { Result = ValidateResultType.ValidateFail, Message = "验证失败" };
}
public static ValidateResult Timeout()
{
return new ValidateResult { Result = ValidateResultType.Timeout, Message = "验证超时" };
}
public enum ValidateResultType
{
Success = 0,
ValidateFail = 1,
Timeout = 2
}
}
}
ICaptcha.cs
在Interface文件夹下,建一个ICaptcha.cs,用于生成验证码和进行校验。
using SlideCaptcha.Model;
using System.Threading.Tasks;
namespace SlideCaptcha.Interface
{
public interface ICaptcha
{
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="captchaId">验证码id</param>
/// <returns></returns>
Task<CaptchaData> GenerateCaptchaImageAsync(string type, string captchaId = null);
/// <summary>
/// 校验
/// </summary>
/// <param name="captchaId">验证码id</param>
/// <param name="slideTrack">滑动轨迹</param>
/// <returns></returns>
ValidateResult Validate(string captchaId, SlideTrack slideTrack);
}
}
IResourceHandler.cs
IResourceHandler.cs 用于生成验证码资源(验证码模板)。
using SlideCaptcha.Model;
using System.Threading.Tasks;
namespace SlideCaptcha.Interface
{
public interface IResourceHandler
{
/// <summary>
/// 判断传入的资源类型是否存在
/// </summary>
/// <param name="handlerType"></param>
/// <returns></returns>
bool CanHandle(string handlerType);
/// <summary>
/// 生成资源
/// </summary>
/// <param name="resource"></param>
/// <returns></returns>
Task<byte[]> Handle(Resource resource);
}
}
IResourceHandlerManager.cs
IResourceHandlerManager.cs用于根据资源(验证码模板),获取资源byte[]数据。
using SlideCaptcha.Model;
using System.Threading.Tasks;
namespace SlideCaptcha.Interface
{
public interface IResourceHandlerManager
{
/// <summary>
/// 根据资源,生成所需要的byte[]
/// </summary>
/// <param name="resource"></param>
/// <returns></returns>
Task<byte[]> Handle(Resource resource);
}
}
IResourceManager.cs
IResourceManager.cs用于获取验证码背景数据与验证码模板数据
using System.Threading.Tasks;
namespace SlideCaptcha.Interface
{
public interface IResourceManager
{
/// <summary>
/// 随机获取验证码图片背景
/// </summary>
/// <returns></returns>
Task<byte[]> RandomBackground();
/// <summary>
/// 随机获取模板
/// </summary>
/// <param name="type">模板类型,滑动、旋转、点选</param>
/// <returns></returns>
Task<(byte[], byte[])> RandomTemplate(string type);
}
}
IResourceProvider.cs
IResourceProvider.cs用于提供验证码背景图片与模板图片的List
using SlideCaptcha.Model;
using System.Collections.Generic;
namespace SlideCaptcha.Interface
{
public interface IResourceProvider
{
/// <summary>
/// 提供验证码背景图片
/// </summary>
/// <returns></returns>
List<Resource> Backgrounds();
/// <summary>
/// 提供模板资源
/// </summary>
/// <returns></returns>
List<TemplatePair> Templates();
}
}
ISliderCaptchaImageGenerator.cs
ISliderCaptchaImageGenerator.cs用于生成滑动验证码
using SlideCaptcha.Model;
using System.Threading.Tasks;
namespace SlideCaptcha.Interface
{
public interface ISliderCaptchaImageGenerator
{
/// <summary>
/// 生成滑动验证码
/// </summary>
/// <param name="captchaId">验证码ID</param>
/// <returns></returns>
Task<SliderImageCaptchaInfo> Generate(string captchaId);
}
}
IStorage.cs
IStorage.cs 用于缓存的操作
using System;
namespace SlideCaptcha.Interface
{
public interface IStorage
{
/// <summary>
/// 设置缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="absoluteExpiration"></param>
void Set<T>(string key, T value, DateTimeOffset absoluteExpiration);
/// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
T Get<T>(string key);
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="key"></param>
void Remove(string key);
}
}
IValidator.cs
IValidator.cs 用于提供校验验证码数据。
using SlideCaptcha.Model;
namespace SlideCaptcha.Interface
{
public interface IValidator
{
/// <summary>
/// 校验验证码数据
/// </summary>
/// <param name="slideTrack">滑动</param>
/// <param name="captchaValidateData">校验</param>
/// <returns></returns>
bool Validate(SlideTrack slideTrack, CaptchaValidateData captchaValidateData);
}
}
下载方式:
点击下方公众号卡片,关注我,回复captcha
免费领取!