C#驗(yàn)證碼的創(chuàng)建與使用示例
本文實(shí)例講述了C#驗(yàn)證碼的創(chuàng)建與使用方法。分享給大家供大家參考,具體如下:
1、C#創(chuàng)建驗(yàn)證碼
① 創(chuàng)建獲取驗(yàn)證碼頁面(ValidateCode.aspx)
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>獲取驗(yàn)證碼</title> </head> <body> <form id="form1" runat="server"> <div>獲取驗(yàn)證碼</div> </form> </body> </html>
② 編寫獲取驗(yàn)證碼代碼(ValidateCode.aspx.cs)
/// <summary> /// 驗(yàn)證碼類型(0-字母數(shù)字混合,1-數(shù)字,2-字母) /// </summary> private string validateCodeType = "0"; /// <summary> /// 驗(yàn)證碼字符個(gè)數(shù) /// </summary> private int validateCodeCount = 4; /// <summary> /// 驗(yàn)證碼的字符集,去掉了一些容易混淆的字符 /// </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"); //獲取設(shè)置參數(shù) if (!string.IsNullOrEmpty(Request.QueryString["validateCodeType"])) { validateCodeType = Request.QueryString["validateCodeType"]; } if (!string.IsNullOrEmpty(Request.QueryString["validateCodeCount"])) { int.TryParse(Request.QueryString["validateCodeCount"], out validateCodeCount); } //生成驗(yàn)證碼 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)]; // 要求全為數(shù)字或字母 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 { //生成隨機(jī)生成器 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); } //畫圖片的前景噪音點(diǎn) 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(); } }
2、驗(yàn)證碼的使用
① 驗(yàn)證碼的前段顯示代碼
② 創(chuàng)建驗(yàn)證碼測試頁面(ValidateTest.aspx)
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>驗(yàn)證碼測試</title> </head> <body> <form id="form1" runat="server"> <div> <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="點(diǎn)擊刷新驗(yàn)證碼" title="點(diǎn)擊刷新驗(yàn)證碼" style="cursor: pointer;"> <asp:Button runat="server" id="btnVal" Text="提交" onclick="btnVal_Click" /> </div> </form> </body> </html>
③ 編寫驗(yàn)證碼測試的提交代碼(ValidateTest.aspx.cs)
protected void btnVal_Click(object sender, EventArgs e) { bool result = false; //驗(yàn)證結(jié)果 string userCode = this.txtValidate.Value; //獲取用戶輸入的驗(yàn)證碼 if (String.IsNullOrEmpty(userCode)) { //請輸入驗(yàn)證碼 return; } string validCode = this.Session["CheckCode"] as String; //獲取系統(tǒng)生成的驗(yàn)證碼 if (!string.IsNullOrEmpty(validCode)) { if (userCode.ToLower() == validCode.ToLower()) { //驗(yàn)證成功 result = true; } else { //驗(yàn)證失敗 result = false; } } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
上一篇:C# listview 點(diǎn)擊列頭排序的實(shí)例
欄 目:C#教程
下一篇:C#編程獲取IP地址的方法示例
本文標(biāo)題:C#驗(yàn)證碼的創(chuàng)建與使用示例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5966.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改