C#驗證碼的建立與使用的範例程式碼分享
這篇文章主要介紹了C驗證碼的創建與使用方法,結合實例形式較為詳細的分析了C#驗證碼的創建、驗證等操作步驟與相關技巧,需要的朋友可以參考下
本文實例講述了C#驗證碼的建立與使用方法。 ##② 編寫取得驗證碼程式碼(ValidateCode.aspx.cs)
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>获取验证码</title> </head> <body> <form id="form1" runat="server"> <p>获取验证码</p> </form> </body> </html>
登入後複製
2、驗證碼的使用
① 驗證碼的前段顯示程式碼
程式碼如下:/// <summary> /// 验证码类型(0-字母数字混合,1-数字,2-字母) /// </summary> private string validateCodeType = "0"; /// <summary> /// 验证码字符个数 /// </summary> private int validateCodeCount = 4; /// <summary> /// 验证码的字符集,去掉了一些容易混淆的字符 /// </summary> char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; protected void Page_Load(object sender, EventArgs e) { //取消缓存 Response.BufferOutput = true; Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1)); Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); Response.AppendHeader("Pragma", "No-Cache"); //获取设置参数 if (!string.IsNullOrEmpty(Request.QueryString["validateCodeType"])) { validateCodeType = Request.QueryString["validateCodeType"]; } if (!string.IsNullOrEmpty(Request.QueryString["validateCodeCount"])) { int.TryParse(Request.QueryString["validateCodeCount"], out validateCodeCount); } //生成验证码 this.CreateCheckCodeImage(GenerateCheckCode()); } private string GenerateCheckCode() { char code ; string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < validateCodeCount; i++) { code = character[random.Next(character.Length)]; // 要求全为数字或字母 if (validateCodeType == "1") { if ((int)code < 48 || (int)code > 57) { i--; continue; } } else if (validateCodeType == "2") { if ((int)code < 65 || (int)code > 90) { i--; continue; } } checkCode += code; } Response.Cookies.Add(new System.Web.HttpCookie("CheckCode", checkCode)); this.Session["CheckCode"] = checkCode; return checkCode; } private void CreateCheckCodeImage(string checkCode) { if (checkCode == null || checkCode.Trim() == String.Empty) return; System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length*15.0+40)), 23); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(System.Drawing.Color.White); //画图片的背景噪音线 for (int i = 0; i < 25; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Silver), x1, y1, x2, y2); } System.Drawing.Font font = new System.Drawing.Font("Arial", 14, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true); int cySpace = 16; for (int i = 0; i < validateCodeCount; i++) { g.DrawString(checkCode.Substring(i, 1), font, brush, (i + 1) * cySpace, 1); } //画图片的前景噪音点 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } }
登入後複製
<img src="/ValidateCode.aspx?ValidateCodeType=1&0.011150883024061309" onclick ="this.src='/ValidateCode.aspx?ValidateCodeType=1&'+Math.random();" id="imgValidateCode" alt="点击刷新验证码" title="点击刷新验证码" style="cursor: pointer;">
登入後複製
③ 編寫驗證碼測試的提交程式碼(ValidateTest.aspx.cs)
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>验证码测试</title> </head> <body> <form id="form1" runat="server"> <p> <input runat="server" id="txtValidate" /> <img src="/ValidateCode.aspx?ValidateCodeType=1&0.011150883024061309" onclick="this.src='/ValidateCode.aspx?ValidateCodeType=1&'+Math.random();" id="imgValidateCode" alt="点击刷新验证码" title="点击刷新验证码" style="cursor: pointer;"> <asp:Button runat="server" id="btnVal" Text="提交" onclick="btnVal_Click" /> </p> </form> </body> </html>
登入後複製
以上是C#驗證碼的建立與使用的範例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前
By DDD
如何修復KB5055523無法在Windows 11中安裝?
2 週前
By DDD
Inzoi:如何申請學校和大學
3 週前
By DDD
如何修復KB5055518無法在Windows 10中安裝?
2 週前
By DDD
Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla
4 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。
