LazyCaptcha自定義隨機(jī)驗(yàn)證碼和字體的示例詳解
介紹
LazyCaptcha是仿EasyCaptcha和SimpleCaptcha,基于.Net Standard 2.1的圖形驗(yàn)證碼模塊。
一. 自定義隨機(jī)驗(yàn)證碼(需要版本1.1.2)
這里隨機(jī)是指CaptchaType隨機(jī),動靜隨機(jī)等等,你可以設(shè)置CaptchaOptions任意選項(xiàng)值。每次刷新驗(yàn)證碼,效果如下:

我也不知道這種需求是否真實(shí)存在。
1. 自定義RandomCaptcha
/// <summary>
/// 隨機(jī)驗(yàn)證碼
/// </summary>
public class RandomCaptcha : DefaultCaptcha
{
private static readonly Random random = new();
private static readonly CaptchaType[] captchaTypes = Enum.GetValues<CaptchaType>();
public RandomCaptcha(IOptionsSnapshot<CaptchaOptions> options, IStorage storage) : base(options, storage)
{
}
/// <summary>
/// 更新選項(xiàng)
/// </summary>
/// <param name="options"></param>
protected override void ChangeOptions(CaptchaOptions options)
// 隨機(jī)驗(yàn)證碼類型
options.CaptchaType = captchaTypes[random.Next(0, captchaTypes.Length)];
// 當(dāng)是算數(shù)運(yùn)算時(shí),CodeLength是指運(yùn)算數(shù)個(gè)數(shù)
if (options.CaptchaType.IsArithmetic())
{
options.CodeLength = 2;
}
else
options.CodeLength = 4;
// 如果包含中文時(shí),使用kaiti字體,否則文字亂碼
if (options.CaptchaType.ContainsChinese())
options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti;
options.ImageOption.FontSize = 24;
options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj;
// 動靜隨機(jī)
options.ImageOption.Animation = random.Next(2) == 0;
// 干擾線隨機(jī)
options.ImageOption.InterferenceLineCount = random.Next(1, 4);
// 氣泡隨機(jī)
options.ImageOption.BubbleCount = random.Next(1, 4);
// 其他選項(xiàng)...
}
2. 注入RandomCaptcha
// 內(nèi)存存儲, 基于appsettings.json配置 builder.Services.AddCaptcha(builder.Configuration); // 開啟隨機(jī)驗(yàn)證碼 builder.Services.Add(ServiceDescriptor.Scoped<ICaptcha, RandomCaptcha>());
二. 自定義字體
使用KG HAPPY字體,效果如圖:

1. 尋找字體
你可以通過fontspace找到自己喜愛的字體。
2. 將字體放入項(xiàng)目,并設(shè)置為嵌入資源。
當(dāng)然也可以不作為嵌入資源,放到特定目錄也是可以的,只要對下邊ResourceFontFamilysFinder稍作修改即可。

3. 定義查找字體幫助類,示例使用ResourceFontFamilysFinder
public class ResourceFontFamilysFinder
{
private static Lazy<List<FontFamily>> _fontFamilies = new Lazy<List<FontFamily>>(() =>
{
var fontFamilies = new List<FontFamily>();
var assembly = Assembly.GetExecutingAssembly();
var names = assembly.GetManifestResourceNames();
if (names?.Length > 0 == true)
{
var fontCollection = new FontCollection();
foreach (var name in names)
{
if (!name.EndsWith("ttf")) continue;
fontFamilies.Add(fontCollection.Add(assembly.GetManifestResourceStream(name)));
}
}
return fontFamilies;
});
public static FontFamily Find(string name)
return _fontFamilies.Value.First(e => e.Name == name);
}
}
4. 設(shè)置option
// 內(nèi)存存儲, 基于appsettings.json配置
builder.Services.AddCaptcha(builder.Configuration, options =>
{
// 自定義字體
options.ImageOption.FontSize = 28;
options.ImageOption.FontFamily = ResourceFontFamilysFinder.Find("KG HAPPY"); // 字體的名字在打開ttf文件時(shí)會顯示
});
到此這篇關(guān)于LazyCaptcha自定義隨機(jī)驗(yàn)證碼和字體的文章就介紹到這了,更多相關(guān)LazyCaptcha隨機(jī)驗(yàn)證碼和字體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ClickOnce DIY全自動更新下載升級的自我實(shí)現(xiàn)
ClickOnce DIY全自動更新下載升級的自我實(shí)現(xiàn)...2007-08-08
DataView.RowFilter的使用(包括in,like等SQL中的操作符)
這篇blog轉(zhuǎn)自C# examples,對DataView.RowFilter做了詳細(xì)介紹,能像SQL中使用in,like等操作符一樣進(jìn)行過濾查詢,并附有實(shí)例,使用方便。2011-07-07
ASP.NET 防止按鈕多次提交核心實(shí)現(xiàn)代碼
防止按鈕多次提交通常都是在注冊表單中提示時(shí)的一個(gè)小功能,具體實(shí)現(xiàn)如下,由此需求的朋友可以參考下2013-08-08
.Net使用Cancellation?Framework取消并行任務(wù)
這篇文章介紹了.Net使用Cancellation?Framework取消并行任務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
ASP.NET?Core擴(kuò)展庫ServiceStack.Redis用法介紹
這篇文章介紹了ASP.NET?Core擴(kuò)展庫ServiceStack.Redis的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02

