小教室:驗證碼的功能:
幾年前,網站、論壇之類的大部分是沒有驗證碼的,因為一般使用者來說驗證碼只是增加了使用者的操作,降低了使用者的體驗。但後來各種灌水機器人、投票機器人、惡意註冊機器人層出不窮,大大增加了網站的負擔同時也為網站資料庫帶來了大量的垃圾資料。為了防止各種機器人程式的破壞,於是程式設計師想出了只有人眼能夠辨識的,程式不容易辨識的驗證碼!
驗證碼是一張圖片,將字母、數字甚至漢字作為圖片的內容,這樣一張圖片中的內容用人眼很容易識別,而程式將無法識別。在進行資料庫操作之前(例如登入驗證、投票、發佈、回覆、註冊等等)程序先驗證用戶端提交的驗證碼是否與圖片中的內容相同,如果相同則進行資料庫操作,則不同則提示驗證碼錯誤,不進行資料庫操作。這樣各種機器人程式就被拒於門外了!
但是隨著電腦科學的發展,模式識別等技術越來越成熟,於是編寫機器人程式的傢伙可以透過程式將直接寫在圖片中的內容識別出來,然後提交到伺服器,這樣驗證碼將形同虛設。為了防止機器人程式的識別,驗證碼的圖片產生也不斷在發展,加入乾擾點、幹擾線,文字變形、變換角度位置,顏色不同…各種防止電腦辨識的技術也應用到驗證碼中。就在這兩種技術的競爭中,於是便形成了我們現在看到的驗證碼,已經有很多人在抱怨“這是什麼驗證碼哦,人眼都分辨不清楚是什麼”,一切也是無奈。
了解了驗證碼的作用,下面寫一個簡單的驗證碼生成及使用的實例
先建個頁面用來展示驗證碼和判斷驗證碼輸入是否正確
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> //点击切换验证码 function f_refreshtype() { var Image1 = document.getElementById("img"); if (Image1 != null) { Image1.src = Image1.src + "?"; } } </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> <td> <img src="/static/imghwm/default1.png" data-src="png.aspx" class="lazy" id="img" onclick="f_refreshtype()" / alt=".net驗證碼產生及使用方法" > </td> <td> <asp:Button ID="Button1" runat="server" Text="确定" /> </td> </tr> </table> </div> </form> </body> </html>
此頁後台對驗證碼進行驗證
protected void Page_Load(object sender, EventArgs e) { //生成的验证码被保存到session中 if (Session["CheckCode"] != null) { string checkcode = Session["CheckCode"].ToString(); if (this.TextBox1.Text == checkcode) { ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入正确!')", true); } else { ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入错误!')", true); } } }
產生驗證碼頁面png.aspx
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CreateCheckCodeImage(GenerateCheckCodes(4)); } } public void ShowAuthCode(Stream stream, out string code) { Random random = new Random(); code = random.Next(1000, 9999).ToString(); Bitmap bitmap = CreateAuthCode(code); bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif); } private string GenerateCheckCodes(int iCount) { int number; string checkCode = String.Empty; int iSeed = DateTime.Now.Millisecond; System.Random random = new Random(iSeed); for (int i = 0; i < iCount; i++) { number = random.Next(10); checkCode += number.ToString(); } Session["CheckCode"] = checkCode; return checkCode; } private Bitmap CreateAuthCode(string str) { Font fn = new Font("宋体", 12); Brush forecolor = Brushes.Black; Brush bgcolor = Brushes.White; PointF pf = new PointF(5, 5); Bitmap bitmap = new Bitmap(100, 25); Rectangle rec = new Rectangle(0, 0, 100, 25); Graphics gh = Graphics.FromImage(bitmap); gh.FillRectangle(bgcolor, rec); gh.DrawString(str, fn, forecolor, pf); return bitmap; } private void CreateCheckCodeImage(string checkCode) { if (checkCode == null || checkCode.Trim() == String.Empty) return; int iWordWidth = 15; int iImageWidth = checkCode.Length * iWordWidth; Bitmap image = new Bitmap(iImageWidth, 20); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音点 for (int i = 0; i < 20; 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 Pen(Color.Silver), x1, y1, x2, y2); } //画图片的背景噪音线 for (int i = 0; i < 2; i++) { int x1 = 0; int x2 = image.Width; int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); if (i == 0) { g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2); } } for (int i = 0; i < checkCode.Length; i++) { string Code = checkCode[i].ToString(); int xLeft = iWordWidth * (i); random = new Random(xLeft); int iSeed = DateTime.Now.Millisecond; int iValue = random.Next(iSeed) % 4; if (iValue == 0) { Font font = new Font("Arial", 13, (FontStyle.Bold | System.Drawing.FontStyle.Italic)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 1) { Font font = new System.Drawing.Font("楷体", 13, (FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 2) { Font font = new System.Drawing.Font("宋体", 13, (System.Drawing.FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 3) { Font font = new System.Drawing.Font("黑体", 13, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true); g.DrawString(Code, font, brush, xLeft, 2); } } //////画图片的前景噪音点 //for (int i = 0; i < 8; i++) //{ // int x = random.Next(image.Width); // int y = random.Next(image.Height); // image.SetPixel(x, y, Color.FromArgb(random.Next())); //} //画图片的边框线 g.DrawRectangle(new Pen(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.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } }
更多.net驗證碼產生及使用方法相關文章請關注PHP中文網!

C#.NET的未來趨勢主要集中在雲計算、微服務、AI和機器學習集成以及跨平台開發三個方面。 1)雲計算和微服務:C#.NET通過Azure平台優化雲環境表現,支持構建高效微服務架構。 2)AI和機器學習集成:借助ML.NET庫,C#開發者可在應用中嵌入機器學習模型,推動智能化應用發展。 3)跨平台開發:通過.NETCore和.NET5 ,C#應用可在Windows、Linux和macOS上運行,擴展部署範圍。

C#.NET開發的最新動態和最佳實踐包括:1.異步編程提高應用響應性,使用async和await關鍵字簡化非阻塞代碼;2.LINQ提供強大查詢功能,通過延遲執行和表達式樹高效操作數據;3.性能優化建議包括使用異步編程、優化LINQ查詢、合理管理內存、提升代碼可讀性和維護性、以及編寫單元測試。

如何利用.NET構建應用?使用.NET構建應用可以通過以下步驟實現:1)了解.NET基礎知識,包括C#語言和跨平台開發支持;2)學習核心概念,如.NET生態系統的組件和工作原理;3)掌握基本和高級用法,從簡單控制台應用到復雜的WebAPI和數據庫操作;4)熟悉常見錯誤與調試技巧,如配置和數據庫連接問題;5)應用性能優化與最佳實踐,如異步編程和緩存。

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。

C#和.NET適用於Web、桌面和移動開發。 1)在Web開發中,ASP.NETCore支持跨平台開發。 2)桌面開發使用WPF和WinForms,適用於不同需求。 3)移動開發通過Xamarin實現跨平台應用。

C#.NET生態系統提供了豐富的框架和庫,幫助開發者高效構建應用。 1.ASP.NETCore用於構建高性能Web應用,2.EntityFrameworkCore用於數據庫操作。通過理解這些工具的使用和最佳實踐,開發者可以提高應用的質量和性能。

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

C#和.NET的結合為開發者提供了強大的編程環境。 1)C#支持多態性和異步編程,2).NET提供跨平台能力和並發處理機制,這使得它們在桌面、Web和移動應用開發中廣泛應用。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

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

Dreamweaver Mac版
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能