網頁的驗證碼

1.首先可以寫一個產生隨機驗證碼的aspx文件,如下產生四位數字:
??? private void Page_Load(object sender, System.EventArgs e)
??? {
??????? this.CreateCheckCodeImage(GenerateCheckCode());
??? }

??? private string GenerateCheckCode()
??? {
??????? int number;
??????? char code;
??????? string checkCode = String.Empty;

??????? System.Random random = new Random();

??????? for (int i = 0; i < 4; i++)
??????? {
??????????? number = random.Next();

????????????code = (char)('0' + (char)(number % 10));
??????????? checkCode += code.ToString();
??????? }

??????? Response.Cookies.Add(new HttpCookie("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 * 12.5)), 22);
??????? Graphics g = Graphics.FromImage(image);

??????? try
??????? {
??????????? //生成隨機生成器
??????????? Random random = new Random();

??????????? //清空圖片背景色
??????????? g.Clear(Color.White);

??????????? //畫圖片的背景噪音線
??????????? for (int i = 0; i < 15; 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);
??????????? }

??????????? 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 < 80; 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.ContentType = "image/Gif";
??????????? Response.BinaryWrite(ms.ToArray());
??????? }
??????? finally
??????? {
??????????? g.Dispose();
??????????? image.Dispose();
??????? }
??? }
這個文件名設為CheckCode,它放在CheckCode.aspx的CheckCode.aspx.cs下就可。
2.然后在需要放驗證圖片的地方加上如下代碼:
<img src="common/CheckCode.aspx" alt=""/>就能產生驗證碼了
3.當需要取得驗證圖片值的時候可參考編寫如下代碼
if (String.Compare(Request.Cookies["CheckCode"].Value, validateTb.Text, true) != 0)
??????? {
??????????? //validateTb為文本框
??????????? validateTb.Focus();
??????????? ErrorMsg.Text = "驗證碼輸入錯誤!";
??????????? return;
??????? }

?

TraceBack:http://community.csdn.net/Expert/topic/5739/5739366.xml?temp=1.18655E-03

轉載于:https://www.cnblogs.com/hdjjun/archive/2008/06/17/1223797.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279288.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279288.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279288.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

榮耀9igoogle模式_iGoogle個性化主頁的6種替代方法

榮耀9igoogle模式iGoogle has less than a year to go before it’s shut down for good on November 1, 2013. While Google seems to think that iGoogle isn’t necessary anymore, there are other services waiting to take its place. iGoogle距離其2013年11月1日永久關閉…

華為堡壘機_安恒信息成為“華為云優秀嚴選合作伙伴”,攜手保障“云上”資產安全訪問...

加快5G持續創新能力&#xff0c;為云計算行業注入新動能。近日&#xff0c;以“智者?同行?共贏”為主題的2020華為云ISV(嚴選)合作伙伴大會在杭州隆重舉行。上百位華為云合作伙伴、行業大咖等專業人士齊聚一堂&#xff0c;探討云計算產業熱門話題。作為華為云重要的生態合作伙…

zip4j實現多線程壓縮

使用的jar包&#xff1a;zip4j_1.3.2.jar 基本功能&#xff1a; 針對ZIP壓縮文件創建、添加、分卷、更新和移除文件 (讀寫有密碼保護的Zip文件) (支持AES 128/256算法加密) (支持標準Zip算法加密) (支持zip64格式) (支持Store(僅打包&#xff0c;默認不壓縮&#xff0c;…

非三星手機無法登錄三星賬號_如何解決所有三星手機的煩惱

非三星手機無法登錄三星賬號Samsung is the biggest manufacturer of Android phones in the world, but that doesn’t mean these handsets are perfect out of the box. In fact, most of these phones have several annoyances initially—here’s how to fix many of thes…

設置單元格填充方式_單元格的選擇及設置單元格格式

數據輸入完畢&#xff0c;接下來可以設置字體、對齊方式、添加邊框和底紋等方式設置單元格格式&#xff0c;從而美化工作表。要對單元格進行設置&#xff0c;首先要選中單元格。選擇單元格選擇單元格是指在工作表中確定活動單元格以便在單元格中進行輸入、修改、設置和刪除等操…

Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 要求找到BST中放錯位置的兩個節點. …

springboot三種過濾功能的使用與比較

若要實現對請求的過濾&#xff0c;有三種方式可供選擇&#xff1a;filter、interceptort和aop。本文主要討論三種攔截器的使用場景與使用方式。 下文中的舉例功能是計算每個請求的從開始到結束的時間&#xff0c;例子來源是慕課網。 一、filter 特點&#xff1a;可以獲取原始的…

后綴的形容詞_構詞法(18)構成形容詞的常見后綴 3

即時練習一、按要求改寫下列單詞。1. Japan →___________ adj. 日本(人)的2. Canton →_________ adj. 廣東(人)的3. Vietnam →__________ adj. 越南(人)的4. Europe →__________ adj. 歐洲(人)的5. India → ________ adj. 印度(人)的6. Africa →_______ adj. 非洲(人)的7…

CentOS 桌面啟動無登錄界面

最近VMWare下搞了2個CentOS 32bit虛擬機, 裝了些軟件之后&#xff0c;都遇到開機無法顯示登錄界面&#xff0c; 僅能看見桌面背景圖的情況。 以下是我搜索很久匯總的方法。 嘗試按 ctrl alt F3(快捷鍵可能有所不同), 由桌面模式進入命令行模式。 直接 startx 報錯&#xf…

批量刪除推文_如何搜索(和刪除)您的舊推文

批量刪除推文“The internet never forgets” is an aphorism that isn’t entirely true, but it’s worth thinking about whenever you post to social media. If you think your Twitter profile needs a bit of a scrub, here’s how to search and delete those old twee…

[USACO13JAN] Cow Lineup (單調隊列,尺取法)

題目鏈接 Solution 尺取法板子,算是復習一波. 題中說最多刪除 \(k\) 種,那么其實就是找一個顏色種類最多為 \(k1\) 的區間; 統計一下其中最多的顏色出現次數. 然后直接尺取法,然后每次對于 \(col[r]\) 進行統計,時間復雜度 \(O(n)\) . Code #include<bits/stdc.h> using …

智能記憶功能nest_如何設置和安裝Nest Protect智能煙霧報警器

智能記憶功能nestIf you want to add a bit more convenience and safety to your home’s smoke alarm setup, the Nest Protect comes with a handful of great features to make that a reality. Here’s how to set it up and what all you can do with it. 如果您想為您的…

網格自適應_ANSYS 非線性自適應(NLAD)網格劃分及應用舉例

文章來源&#xff1a;安世亞太官方訂閱號&#xff08;搜索&#xff1a;Peraglobal&#xff09;在復雜的結構設計分析中&#xff0c;通常很難確定在高應力區域中是否生成適當的細化網格。在做非線性大應變分析仿真時&#xff0c;可能由于單元變形過大&#xff0c;導致網格畸變&a…

js繼承優化

在看《js設計模式》中&#xff0c;作者提到了js中的兩種繼承方式&#xff1a;類繼承 或 原型繼承&#xff0c;或許是本人才疏學淺&#xff0c;竟發現一些問題。 一、類繼承 思路&#xff1a;作者的思路是使用基于類來繼承&#xff0c;并且做了一個extend函數&#xff0c;在第一…

python---[列表]lsit

內置數據結構&#xff08;變量類型&#xff09; -list -set -dict -tuple -list&#xff08;列表&#xff09; -一組又順序的數據組合 -創建列表 -空列表 list1 []        print(type(list1))        print(list1)        list2 [100]       …

喚醒計算機運行此任務_如何停止Windows 8喚醒計算機以運行維護

喚醒計算機運行此任務Windows 8 comes with a new hybrid boot system, this means that your PC is never really off. It also means that Windows has the permission to wake your PC as it needs. Here’s how to stop it from waking up your PC to do maintenance tasks…

轉整型_SPI轉can芯片CSM300詳解、Linux驅動移植調試筆記

一口君最近移植了一款SPI轉CAN的芯片CSM300A&#xff0c;在這里和大家做個分享。一、CSM300概述CSM300(A)系列是一款可以支持 SPI / UART 接口的CAN模塊。1. 簡介CSM300(A)系列隔離 SPI / UART 轉 CAN 模塊是集成微處理器、 CAN 收發器、 DC-DC 隔離電源、 信號隔離于一體的通信…

matlab練習程序(二值圖像連通區域標記法,一步法)

這個只需要遍歷一次圖像就能夠完全標記了。我主要參考了WIKI和這位兄弟的博客&#xff0c;這兩個把原理基本上該介紹的都介紹過了&#xff0c;我也不多說什么了。一步法代碼相比兩步法真是清晰又好看&#xff0c;似乎真的比兩步法要好很多。 代碼如下&#xff1a; clear all; c…

pc微信不支持flash_在出售PC之前,如何取消對Flash內容的授權

pc微信不支持flashWhen it comes to selling your old digital equipment you usually should wipe it of all digital traces with something like DBAN, however if you can’t there are some precautions you should take–here’s one related to Flash content you may h…

博客在線——Wireshark基本用法

http://blog.jobbole.com/ http://blog.jobbole.com/70907/轉載于:https://www.cnblogs.com/zhongbokun/p/9709326.html