C#生成圖形驗證碼的實現(xiàn)方式
應(yīng)用場景
我們當(dāng)用戶登錄系統(tǒng)時經(jīng)常會用到圖形驗證碼技術(shù),要求用戶識別圖片中的內(nèi)容,并正確輸入,方可嘗試登錄。類似的場景還有用戶注冊或者涉及頻繁敏感操作的提交表單。
因此,圖形驗證碼是一個網(wǎng)絡(luò)安全技術(shù)手段,防止惡意程序自動攻擊執(zhí)行。盡量能夠避免非法用于訪問和操作受保護(hù)的資源。圖形驗證碼的呈現(xiàn)形式有很多種,這里我們將介紹最基本的生成方式,字母及數(shù)字的呈現(xiàn)方式。
開發(fā)運行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
.net版本: .netFramework4.0 或以上
開發(fā)工具:VS2019 C#
設(shè)計
生成內(nèi)容
GenerateCheckCode 方法用于生成驗證碼,本方法返回 string 類型字符串,其參數(shù)詳見下表:
序號 | 參數(shù)名 | 類型 | 說明 |
---|---|---|---|
1 | charCount | int | 要生成的字母或數(shù)字或組合的總計的字符數(shù)量 |
2 | generateType | GenerateType | 生成類型枚舉,詳見枚舉說明表 |
GenerateType 枚舉說明:
序號 | 枚舉 | 說明 |
---|---|---|
1 | OnlyNumber | 僅生成數(shù)字內(nèi)容 |
2 | OnlyEnLetter | 僅生成字母內(nèi)容 |
3 | Mixed | 生成數(shù)字加字母的組合內(nèi)容 |
生成圖片
CreateCheckCodeImage 方法根據(jù)傳遞生成的字符串生成最終的圖片,圖片數(shù)據(jù)為二進(jìn)制輸出。
其參數(shù)說明如下:
實現(xiàn)
核心代碼
完整的代碼如下:
<%@ Page Language="C#" Debug="False" AspCompat="False"%> <script language="C#" runat="server"> void Page_load(object Sander,EventArgs e) { string vnum=GenerateCheckCode(6,CosysJaneCommonAPI.Security.GenerateType.OnlyNumber); CreateCheckCodeImage(vnum); } public enum GenerateType { OnlyNumber,OnlyEnLetter,Mixed } public string GenerateCheckCode(int charCount,GenerateType generateType) { int number; char code; string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < charCount; i++) { number = random.Next(); if (number % 2 == 0) code = (char)(generateType == GenerateType.OnlyEnLetter ? 'A' : '0' + (char)(number % 10)); else code = (char)(generateType==GenerateType.OnlyNumber?'0':'A' + (char)(number % 26)); checkCode += code.ToString(); } return checkCode; } public 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 * 12.5)), 22); Graphics g = Graphics.FromImage(image); try { //生成隨機生成器 Random random = new Random(); //清空圖片背景色 g.Clear(Color.BurlyWood); //畫圖片的背景噪音線 for (int i = 0; i < 1; 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.Blue), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 2); //畫圖片的前景噪音點 for (int i = 0; i < 20; 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 - 2, 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(); } } </script> <html> <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" > <form runat=server></form> </body> </html>
調(diào)用示例
我們假設(shè)在登錄頁面上放置 Image控件 image1,并將核心代碼保存為 GCCI.aspx文件,則調(diào)用示例如下:
<html> <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" > <form runat=server> <asp:Image ID="image1" runat="server" ImageUrl="GCCI.aspx" /> </form> </body> </html>
通過給 image1 控件的 ImageUrl 賦值為GCCI.aspx文件即可。
顯示成功后如下圖所示:
1、僅數(shù)字模式
2、僅字母模式
3、字母數(shù)字混合模式
小結(jié)
本小結(jié)僅以生成圖形驗證碼為參考,實際的應(yīng)用中我們還需要考慮預(yù)生成驗證碼字符串,進(jìn)行輸入框文字對比操作。
生成驗證碼方法可以進(jìn)一步改造生成符合我們需要的場景(如干擾線復(fù)雜度等)
還可考慮有效時效,過期需要重新刷新生成圖形驗證碼,進(jìn)一步提高安全性。
本示例 ImageUrl 屬性傳遞的是固定URL,若要實現(xiàn)枚舉,請按需要傳遞參數(shù)或其它設(shè)計。
以上就是C#生成圖形驗證碼的實現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于C#圖形驗證碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#數(shù)組學(xué)習(xí)相關(guān)資料整理
最近開始學(xué)習(xí)c#,并有幸接觸到了數(shù)組方便的操作,感覺確實不錯,這里簡單的整理下c#相關(guān)的學(xué)習(xí)資料,方便大家學(xué)習(xí)2012-09-09C#使用InstallerProjects打包桌面應(yīng)用程序的完整步驟
這篇文章主要給大家介紹了關(guān)于C#使用InstallerProjects打包桌面應(yīng)用程序的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Unity Shader實現(xiàn)動態(tài)過場切換圖片效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實現(xiàn)動態(tài)過場切換圖片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07