一:背景 1.講故事 前幾天有位朋友找到我,說他的程式出現了偶發性崩潰,已經抓到了dump文件,Windows事件日誌顯示的崩潰點在 clr.dll 中,讓我幫忙看下是怎麼回事,那到底怎麼回事呢? 上 WinDbg 說話。 二:WinDbg 分析 1. 崩潰點在哪裡 如果是托管代碼引發的崩潰,線上程 ...
這一節,我們在Constant目錄中,定義兩個類CaptchaOptions.cs與CaptchaTypeConstant。目的是設置驗證碼的類型與其他一些屬性。然後在Storage目錄中,設置驗證碼的緩存數據。
上一節內容:.NET 6 實現滑動驗證碼(四)、擴展類
目錄
CaptchaOptions.cs
在Constant 文件夾下新建立CaptchaOptions.cs。定義驗證碼過期時間、緩存key值、容錯值等。
using SlideCaptcha.Model;
using System.Collections.Generic;
namespace SlideCaptcha.Constant
{
public class CaptchaOptions
{
/// <summary>
/// 過期時長
/// </summary>
public int ExpirySeconds { get; set; } = 60;
/// <summary>
/// 存儲鍵首碼
/// </summary>
public string StoreageKeyPrefix { get; set; } = "slide-captcha";
/// <summary>
/// 容錯值(校驗時用,缺口位置與實際滑動位置匹配容錯範圍)
/// </summary>
public float Tolerant { get; set; } = 0.02f;
/// <summary>
/// 背景圖
/// </summary>
public List<Resource> Backgrounds { get; set; } = new List<Resource>();
/// <summary>
/// 模板圖(必須是slider,notch的順序依次出現)
/// </summary>
public List<TemplatePair> Templates { get; set; } = new List<TemplatePair>();
}
}
CaptchaTypeConstant.cs
在Constant 文件夾下新建立CaptchaTypeConstant.cs。定義各種常見驗證碼類型:
namespace SlideCaptcha.Constant
{
public class CaptchaTypeConstant
{
/** 滑塊. */
public static string SLIDER = "SLIDER";
/** 旋轉. */
public static string ROTATE = "ROTATE";
/** 拼接.*/
public static string CONCAT = "CONCAT";
/** 圖片點選.*/
public static string IMAGE_CLICK = "IMAGE_CLICK";
/** 文字圖片點選.*/
public static string WORD_IMAGE_CLICK = "WORD_IMAGE_CLICK";
}
}
本次我們只實現滑塊驗證碼。旋轉、拼接、圖片點選,文字圖片點選暫不考慮。不過在源碼中,已添加了旋轉驗證碼的實現,目前還在測試中。
DefaultStorage.cs
在Storage文件夾建立DefaultStorage.cs,定義緩存的各種方法。
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using SlideCaptcha.Constant;
using SlideCaptcha.Interface;
using System;
using System.Text;
namespace SlideCaptcha.Storage
{
public class DefaultStorage : IStorage
{
private readonly IDistributedCache _cache;
private readonly IOptionsMonitor<CaptchaOptions> _options;
public DefaultStorage(IOptionsMonitor<CaptchaOptions> options, IDistributedCache cache)
{
_options = options;
_cache = cache;
}
private string WrapKey(string key)
{
return $"{this._options.CurrentValue.StoreageKeyPrefix}{key}";
}
public T Get<T>(string key)
{
var bytes = _cache.Get(WrapKey(key));
if (bytes == null) return default(T);
var json = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
return JsonConvert.DeserializeObject<T>(json);
}
public void Remove(string key)
{
_cache.Remove(WrapKey(key));
}
public void Set<T>(string key, T value, DateTimeOffset absoluteExpiration)
{
string json = JsonConvert.SerializeObject(value);
byte[] bytes = Encoding.UTF8.GetBytes(json);
_cache.Set(WrapKey(key), bytes, new DistributedCacheEntryOptions
{
AbsoluteExpiration = absoluteExpiration
});
}
}
}
緩存使用的是IDistributedCache 介面,方便集成在項目的時候,選擇需要的緩存,如MemoryCache或Redis。
下一篇,開始寫驗證碼圖片與驗證碼凹槽的獲取。
歡迎大家關註我的微信公眾號,一起進步,一起成長下載方式:
掃描公眾號二維碼關註我,回覆captcha
下載,壓縮包包含了驗證碼類庫、服務端API、HTML+JQuery完整代碼、vue3組件代碼及演示代碼!
![](https://img2023.cnblogs.com/blog/93324/202212/93324-20221205182127693-712634750.png)