網站首頁 編程語言 正文
作業:驗證碼
要求:
(1)驗證碼應該是圖片格式,不能是文字格式,即無法用鼠標選中。
(2)驗證碼上應該有噪點和干擾線條。
(3)驗證碼應該回避相似字符,如“0”和“o”“I”和“1”等。
(4)驗證碼至少是數字和字母(含大小寫)的組合,不應該是單純的數字或字母,可以出現漢字。應該實現輸入驗證碼字母字母“不區分大小寫”。驗證碼中的內容,應該是隨機生成。
(5)驗證碼,可以通過點擊圖片或旁邊文字實現“看不清,換一張”的功能。
(6)應當有個用于核對驗證碼輸入是否正確的tbx和btn,當點擊btn時,彈窗提示驗證碼輸入的是正確或錯誤
新建一個ashx文件
前端頁面
<asp:Label ID="Label1" runat="server" Text="請輸入驗證碼:" ></asp:Label> <asp:TextBox ID="tbx_check" runat="server"></asp:TextBox> <asp:ImageButton ID="ImageButton1" runat="server" src="checknumber.ashx" alt="驗證碼"/> <asp:Button ID="btn_check" runat="server" Text="驗證" OnClick="btn_check_Click" />
aspx代碼
protected void btn_check_Click(object sender, EventArgs e) { //不區分大小寫驗證 if (String.Compare(tbx_check.Text.Trim(), Session["check"].ToString(), true) == 0) { Response.Write("<script>alert('驗證碼正確') </script>"); } else { Response.Write("<script>alert('驗證碼錯誤') </script>"); } } //點擊圖片更換驗證碼 protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { ImageButton1.ImageUrl = "checknumber.ashx?id=" + new Random(100).ToString(); }
ashx代碼
using System; using System.Collections.Generic; using System.Linq; using System.Web; //畫板 using System.Drawing; using System.Drawing.Imaging; //Session傳值 using System.Web.SessionState; namespace 驗證碼模塊 { /// <summary> /// Handler1 的摘要說明 /// </summary> public class Handler1 : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { //定義位圖的寬度和高度 int iBmpW = 200; int iBmpS = 50; //創建位圖對象(并初始化寬度和高度) Bitmap Bmp = new Bitmap(iBmpW, iBmpS); //創建畫布 Graphics Grc = Graphics.FromImage(Bmp);//將位圖裝載到畫布里面 //將畫布的背景色改為白色 Grc.Clear(Color.White); //定義畫筆對象 SolidBrush solidBrush = new SolidBrush(Color.Blue); //定義存放的字符串,畫布上要顯示的驗證碼,回避相似字符所以去掉I、1、o、0 string sString = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghyzklmnopqrstuvwxtz23456789呆鍋工醬"; //定義隨機數 Random Rnd = new Random((int)DateTime.Now.Ticks);//強制轉換為整型 //for存放隨機數的字符變量 string scode = null; for (int i = 0; i < 6; i++) { string temp = sString.Substring(Rnd.Next(0, sString.Length), 1);//將隨即得到的每個字符進行字符串構造 scode += temp; } //隨機輸出噪點 Random rand = new Random(); for (int i = 0; i < 10; i++) { int x = rand.Next(Bmp.Width); int y = rand.Next(Bmp.Height); Grc.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); } int z = 6;//干擾線條數 for (int i = 0; i < z; i++) { int x1 = rand.Next(Bmp.Width); int x2 = rand.Next(Bmp.Width); int y1 = rand.Next(Bmp.Height); int y2 = rand.Next(Bmp.Height); Grc.DrawLine(new Pen(Color.LightGray, 1), x1, y1, x2, y2);//根據坐標畫線 } Font ft = new Font("Arial", 25); Grc.DrawString(scode, ft, solidBrush, 0, 0); context.Response.ContentType = "image/jpg"; Bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg); //向外傳scode的值以驗證 context.Session["check"] = scode; } public bool IsReusable { get { return false; } } } }
原文鏈接:https://blog.csdn.net/rainknight666/article/details/123717026
相關推薦
- 2022-12-31 Kotlin?Jetpack組件ViewModel使用詳解_Android
- 2022-06-22 Android實現歡迎滑動頁面_Android
- 2022-07-07 淺談Qt實現HTTP的Get/Post請求_C 語言
- 2023-04-24 Python?相對路徑報錯:"No?such?file?or?directory"'原因及解決方法_
- 2022-06-17 Android自定義實現可回彈的ScollView_Android
- 2024-03-20 解決npm install遇到的問題:Error while executing:
- 2022-03-25 ASP.NET?Core實時庫SignalR簡介及使用_實用技巧
- 2022-08-19 MapReduce讀取定長文件入庫Hive表Orc格式
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支