netcore 生成驗證碼的過程詳解
更新時間:2024年06月21日 10:46:37 作者:Session_MY
這篇文章主要介紹了netcore 生成驗證碼的過程詳解,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
安裝依賴
Install-Package Lazy.Captcha.Core
注冊服務(wù)
builder.Services.AddCaptcha();
自定義注冊服務(wù)
// 注冊服務(wù)的時候增加配置
services.AddCaptcha(Configuration, option =>
{
option.CaptchaType = CaptchaType.WORD; // 驗證碼類型
option.CodeLength = 6; // 驗證碼長度, 要放在CaptchaType設(shè)置后. 當類型為算術(shù)表達式時,長度代表操作的個數(shù)
option.ExpirySeconds = 30; // 驗證碼過期時間
option.IgnoreCase = true; // 比較時是否忽略大小寫
option.StoreageKeyPrefix = ""; // 存儲鍵前綴
option.ImageOption.Animation = true; // 是否啟用動畫
option.ImageOption.FrameDelay = 30; // 每幀延遲,Animation=true時有效, 默認30
option.ImageOption.Width = 150; // 驗證碼寬度
option.ImageOption.Height = 50; // 驗證碼高度
option.ImageOption.BackgroundColor = SkiaSharp.SKColors.White; // 驗證碼背景色
option.ImageOption.BubbleCount = 2; // 氣泡數(shù)量
option.ImageOption.BubbleMinRadius = 5; // 氣泡最小半徑
option.ImageOption.BubbleMaxRadius = 15; // 氣泡最大半徑
option.ImageOption.BubbleThickness = 1; // 氣泡邊沿厚度
option.ImageOption.InterferenceLineCount = 2; // 干擾線數(shù)量
option.ImageOption.FontSize = 36; // 字體大小
option.ImageOption.FontFamily = DefaultFontFamilys.Instance.Actionj; // 字體
/*
* 中文使用kaiti,其他字符可根據(jù)喜好設(shè)置(可能部分轉(zhuǎn)字符會出現(xiàn)繪制不出的情況)。
* 當驗證碼類型為“ARITHMETIC”時,不要使用“Ransom”字體。(運算符和等號繪制不出來)
*/
option.ImageOption.TextBold = true;// 粗體,該配置2.0.3新增
});提供一個生成驗證碼和校驗的接口
/// <summary>
/// 生成驗證碼
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Captcha(string id)
{
var info = _captcha.Generate(id);
// 有多處驗證碼且過期時間不一樣,可傳第二個參數(shù)覆蓋默認配置。
//var info = _captcha.Generate(id,120);
var stream = new MemoryStream(info.Bytes);
return File(stream, "image/gif");
}
/// <summary>
/// 驗證驗證碼是否輸入正確
/// </summary>
[HttpGet]
public bool Validate(string id, string code)
{
return _captcha.Validate(id, code);
}前端部分
<div class="text-center">
<img id="captchaImage" src="" alt="Captcha Image" />
<input type="text" id="captchaInput" placeholder="Enter the captcha code" />
<button id="validateButton">Validate</button>
<div id="resultMessage"></div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
var guid = generateUUIDv4();
// 加載驗證碼圖片
loadCaptcha();
// 監(jiān)聽驗證按鈕點擊事件
document.getElementById('validateButton').addEventListener('click', validateCaptcha);
// 點擊圖片刷新驗證碼
document.getElementById('captchaImage').addEventListener('click', captchaImageClick);
async function captchaImageClick() {
//刷新GUID
guid = generateUUIDv4();
// 重新加載驗證碼
loadCaptcha();
}
async function loadCaptcha() {
try {
const response = await fetch('/home/Captcha?id=' + guid);
const blob = await response.blob();
const imageUrl = URL.createObjectURL(blob);
document.getElementById('captchaImage').src = imageUrl;
} catch (error) {
console.error('Failed to load captcha:', error);
}
}
async function validateCaptcha() {
const input = document.getElementById('captchaInput').value;
const response = await fetch('/home/Validate?id=' + guid + "&code=" + input);
const data = await response.json();
const messageElement = document.getElementById('resultMessage');
if (data) {
messageElement.textContent = 'Captcha is correct!';
messageElement.style.color = 'green';
} else {
messageElement.textContent = 'Incorrect captcha. Please try again.';
messageElement.style.color = 'red';
//刷新GUID
guid = generateUUIDv4();
// 重新加載驗證碼
loadCaptcha();
}
}
function generateUUIDv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
});
</script>
代碼地址:GitHub - maoyuan6/verificationcode: 驗證碼
到此這篇關(guān)于netcore 生成驗證碼的文章就介紹到這了,更多相關(guān)netcore 生成驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VS2015 搭建Asp.net core開發(fā)環(huán)境的方法
最近想在vs2015體驗下.net core,折騰了兩天終于把環(huán)境弄好了。下面這篇文章就給大家分享下我的搭建過程,有需要的朋友們可以參考學習,下面來一起看看吧。2016-12-12
ASP.NET 2.0服務(wù)器控件開發(fā)之復雜屬性
ASP.NET 2.0服務(wù)器控件開發(fā)之復雜屬性...2006-09-09
淺析如何在?ASP.NET?Core中實現(xiàn)速率限制
在?ASP.NET?Core?中實現(xiàn)速率限制(Rate?Limiting)中間件可以幫助你控制客戶端對?API?的請求頻率,防止濫用和過載,下面我們就來看看具體實現(xiàn)方法吧2025-01-01

