題外話,有網友說,這玩意根本很容易破解,確實是這樣。但驗證碼這東西,就跟鎖子很類似,防君子不防小人。驗證碼的發明其實是社會文明的退步。因為它阻礙了真正的使用者,卻無法阻擋別有用心的人。但又有什麼辦法呢?
上一篇文章新增了一些基本類,本篇內容主要目的是建立一些介面類
上一節內容:NET 6 實現滑動驗證碼(二)、基本資料
在專案根目錄下,建立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
}
}
}
在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 用於生成驗證碼資源(驗證碼模板)。
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用於根據資源(驗證碼模板),獲取資源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用於獲取驗證碼背景資料與驗證碼模板資料
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用於提供驗證碼背景圖片與模板圖片的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用於生成滑動驗證碼
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 用於快取的操作
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 用於提供校驗驗證碼資料。
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);
}
}
下載地址(包括驗證碼原始碼、伺服器端API範例、前端程式碼有HTML+JQyeryt、vue3兩個範例)
https://pan.baidu.com/s/19mx24FXrnqz9u2mmFqlr6g?pwd=7636