日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

LazyCaptcha自定義隨機驗證碼和字體的示例詳解_實用技巧

作者:pojianbing ? 更新時間: 2022-05-22 編程語言

介紹

LazyCaptcha是仿EasyCaptcha和SimpleCaptcha,基于.Net Standard 2.1的圖形驗證碼模塊。

一. 自定義隨機驗證碼(需要版本1.1.2)

這里隨機是指CaptchaType隨機,動靜隨機等等,你可以設置CaptchaOptions任意選項值。每次刷新驗證碼,效果如下:

我也不知道這種需求是否真實存在。

1. 自定義RandomCaptcha

/// 
/// 隨機驗證碼
/// 
public class RandomCaptcha : DefaultCaptcha
{
    private static readonly Random random = new();
    private static readonly CaptchaType[] captchaTypes = Enum.GetValues();
    public RandomCaptcha(IOptionsSnapshot options, IStorage storage) : base(options, storage)
    {
    }
    /// 
    /// 更新選項
    /// 
    /// 
    protected override void ChangeOptions(CaptchaOptions options)
        // 隨機驗證碼類型
        options.CaptchaType = captchaTypes[random.Next(0, captchaTypes.Length)];
        // 當是算數運算時,CodeLength是指運算數個數
        if (options.CaptchaType.IsArithmetic())
        {
            options.CodeLength = 2;
        }
        else
            options.CodeLength = 4;
        // 如果包含中文時,使用kaiti字體,否則文字亂碼
        if (options.CaptchaType.ContainsChinese())
            options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti;
            options.ImageOption.FontSize = 24;
            options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj;
        // 動靜隨機
        options.ImageOption.Animation = random.Next(2) == 0;
        // 干擾線隨機
        options.ImageOption.InterferenceLineCount = random.Next(1, 4);
        // 氣泡隨機
        options.ImageOption.BubbleCount = random.Next(1, 4);
        // 其他選項...
}

2. 注入RandomCaptcha

// 內存存儲, 基于appsettings.json配置
builder.Services.AddCaptcha(builder.Configuration);
// 開啟隨機驗證碼
builder.Services.Add(ServiceDescriptor.Scoped());

二. 自定義字體

使用KG HAPPY字體,效果如圖:

1. 尋找字體

你可以通過fontspace找到自己喜愛的字體。

2. 將字體放入項目,并設置為嵌入資源。

當然也可以不作為嵌入資源,放到特定目錄也是可以的,只要對下邊ResourceFontFamilysFinder稍作修改即可。

3. 定義查找字體幫助類,示例使用ResourceFontFamilysFinder

public class ResourceFontFamilysFinder
{
    private static Lazy> _fontFamilies = new Lazy>(() =>
    {
        var fontFamilies = new List();
        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. 設置option

// 內存存儲, 基于appsettings.json配置
builder.Services.AddCaptcha(builder.Configuration, options =>
{
    // 自定義字體
    options.ImageOption.FontSize = 28;
    options.ImageOption.FontFamily = ResourceFontFamilysFinder.Find("KG HAPPY"); // 字體的名字在打開ttf文件時會顯示
});

原文鏈接:https://www.cnblogs.com/readafterme/p/16012880.html

欄目分類
最近更新