自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET1.1驗證碼產(chǎn)生的原理及應(yīng)用

開發(fā) 后端
文章介紹了asp.net1.1中驗證碼產(chǎn)生的原理及應(yīng)用。實現(xiàn)原理就是,通過隨機函數(shù)產(chǎn)生驗證碼元素,將數(shù)值型的驗證碼元素轉(zhuǎn)換成字符型然后再連成字符串,將驗證碼字符串寫入Cookie以供驗證時調(diào)用。

ASP.NET1.1驗證碼實現(xiàn)原理

通過隨機函數(shù)產(chǎn)生驗證碼元素,將數(shù)值型的驗證碼元素轉(zhuǎn)換成字符型然后再連成字符串,將驗證碼字符串寫入Cookie以供驗證時調(diào)用。

通過后臺動態(tài)繪制位圖的方法,繪制一個指定大小的位圖,然后在空白位圖畫出底紋、驗證碼字體、和邊框。

ASP.NET1.1驗證碼實現(xiàn)代碼

(1)Login.aspx(登錄頁前臺)

  1. < %@ Page language="c#" Codebehind="Login.aspx.cs" AutoEventWireup="false" Inherits="Validator.Login" %>  
  2. < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >  
  3. < HTML>  
  4.     < HEAD>  
  5.         < title>Login< /title>  
  6.         < meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">  
  7.         < meta name="CODE_LANGUAGE" Content="C#">  
  8.         < meta name="vs_defaultClientScript" content="JavaScript">  
  9.         < meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">  
  10.     < /HEAD>  
  11.     < body MS_POSITIONING="GridLayout">  
  12.         < form id="Form1" method="post" runat="server">  
  13.             < asp:button id="Button1" style="Z-INDEX: 101; LEFT: 128px; POSITION: absolute; TOP: 64px" runat="server" 
  14.                 Width="96px" Text="提交">< /asp:button>  
  15.             < IMG src="CheckCode.aspx">  
  16.             < asp:label id="lblMessage" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 128px" 
  17.                 runat="server">< /asp:label>  
  18.             < asp:textbox id="txtCheckCode" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 64px" 
  19.                 runat="server" Width="88px">< /asp:textbox>  
  20.         < /form>  
  21.     < /body>  
  22. < /HTML>  

(2)Login.aspx.cs(登錄頁后臺)

  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.HtmlControls;  
  11.  
  12. namespace Validator  
  13. {  
  14.     /**//// < summary>  
  15.     /// Login 的摘要說明。  
  16.     /// < /summary>  
  17.     public class Login : System.Web.UI.Page  
  18.     {  
  19.         protected System.Web.UI.WebControls.Button Button1;  
  20.         protected System.Web.UI.WebControls.Label lblMessage;  
  21.         protected System.Web.UI.WebControls.TextBox txtCheckCode;  
  22.       
  23.         private void Page_Load(object sender, System.EventArgs e)  
  24.         {  
  25.             // 在此處放置用戶代碼以初始化頁面  
  26.         }  
  27.  
  28.         Web 窗體設(shè)計器生成的代碼#region Web 窗體設(shè)計器生成的代碼  
  29.         override protected void OnInit(EventArgs e)  
  30.         {  
  31.             //  
  32.             // CODEGEN: 該調(diào)用是 asp.net Web 窗體設(shè)計器所必需的。  
  33.             //  
  34.             InitializeComponent();  
  35.             base.OnInit(e);  
  36.         }  
  37.           
  38.         /**//// < summary>  
  39.         /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改  
  40.         /// 此方法的內(nèi)容。  
  41.         /// < /summary>  
  42.         private void InitializeComponent()  
  43.         {      
  44.             this.Button1.Click += new System.EventHandler(this.Button1_Click);  
  45.             this.Load += new System.EventHandler(this.Page_Load);  
  46.  
  47.         }  
  48.         #endregion  
  49.  
  50.         private void Button1_Click(object sender, System.EventArgs e)  
  51.         {  
  52.             if(Request.Cookies["CheckCode"] == null)   
  53.             {  
  54.                 lblMessage.Text = "您的瀏覽器設(shè)置已被禁用 Cookies,您必須設(shè)置瀏覽器允許使用 Cookies 選項后才能使用本系統(tǒng)。";  
  55.                 lblMessage.Visible = true;  
  56.                 return;  
  57.             }  
  58.             if(String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, false) != 0) //參數(shù)為false時為區(qū)分大小寫  
  59.             {  
  60.                 lblMessage.Text = "驗證碼錯誤,請輸入正確的驗證碼。";  
  61.                 lblMessage.Visible = true;  
  62.                 return;  
  63.             }   
  64.             else   
  65.             {  
  66.                 lblMessage.Text = "通過驗證";  
  67.                 lblMessage.Visible = true;  
  68.                 return;  
  69.             }  
  70.         }  
  71.     }  
  72. }  
  73.  

(3) CheckCode.aspx(驗證頁前臺)

  1. < %@ Page language="c#" Codebehind="CheckCode.aspx.cs" AutoEventWireup="false" Inherits="Validator.CheckCode" %>  
  2. < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >   
  3.  
  4. < html>  
  5.   < head>  
  6.     < title>CheckCode< /title>  
  7.     < meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">  
  8.     < meta name="CODE_LANGUAGE" Content="C#">  
  9.     < meta name=vs_defaultClientScript content="JavaScript">  
  10.     < meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">  
  11.   < /head>  
  12.   < body MS_POSITIONING="GridLayout">  
  13.       
  14.     < form id="Form1" method="post" runat="server">  
  15.         < FONT face="宋體">< /FONT>  
  16.     < /form>  
  17.       
  18.   < /body>  
  19. < /html>  
  20.  

(4)CheckCode.aspx.cs(驗證頁后臺)

  1. using System;  
  2. using System.Collections;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Web;  
  7. using System.Web.SessionState;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.HtmlControls;  
  11.  
  12. namespace Validator  
  13. {  
  14.     /**//// < summary>  
  15.     /// CheckCode 的摘要說明。  
  16.     /// < /summary>  
  17.     public class CheckCode : System.Web.UI.Page  
  18.     {  
  19.         private void Page_Load(object sender, System.EventArgs e)  
  20.         {  
  21.             // 在此處放置用戶代碼以初始化頁面  
  22.             this.CreateCheckCodeImage(GenerateCheckCode());  
  23.         }  
  24.  
  25.         Web 窗體設(shè)計器生成的代碼#region Web 窗體設(shè)計器生成的代碼  
  26.         override protected void OnInit(EventArgs e)  
  27.         {  
  28.             //  
  29.             // CODEGEN: 該調(diào)用是 asp.net Web 窗體設(shè)計器所必需的。  
  30.             //  
  31.             InitializeComponent();  
  32.             base.OnInit(e);  
  33.         }  
  34.           
  35.         /**//// < summary>  
  36.         /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改  
  37.         /// 此方法的內(nèi)容。  
  38.         /// < /summary>  
  39.         private void InitializeComponent()  
  40.         {      
  41.             this.Load += new System.EventHandler(this.Page_Load);  
  42.         }  
  43.         #endregion  
  44.  
  45.         private string GenerateCheckCode()  
  46.         {                        
  47.             int number;                        
  48.             char code;                        
  49.             string checkCode = String.Empty;                                     
  50.             System.Random random = new Random();                         
  51.             for(int i=0; i< 5; i++)                        
  52.             {   //隨機產(chǎn)生一個整數(shù)                               
  53.                 number = random.Next();                   
  54.                 //如果隨機數(shù)是偶數(shù) 取余選擇從[0-9]                               
  55.                 if(number % 2 == 0)                                      
  56.                     code = (char)('0' + (char)(number % 10));                               
  57.                 else                               
  58.                 //如果隨機數(shù)是奇數(shù) 選擇從[A-Z]                                      
  59.                     code = (char)('A' + (char)(number % 26));                                
  60.                     checkCode += code.ToString();                        
  61.             }                         
  62.             Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));                         
  63.             return checkCode;                 
  64.         }           
  65.  
  66.         //建立一個隨機圖形                 
  67.         private void CreateCheckCodeImage(string checkCode)                 
  68.         {                        
  69.             if(checkCode == null || checkCode.Trim() == String.Empty)                               
  70.                 return;               
  71.             //建立一個位圖文件 確立長寬                                         
  72.             System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);                        
  73.             Graphics g = Graphics.FromImage(image);                         
  74.             try                        
  75.             {                               
  76.                 //生成隨機生成器                               
  77.                 Random random = new Random();                                
  78.                 //清空圖片背景色                               
  79.                 g.Clear(Color.White);                                
  80.                 //畫圖片的背景噪音點                               
  81.                 for(int i=0; i< 60; i++)                               
  82.                 {                                      
  83.                     int x = random.Next(image.Width);                                      
  84.                     int y = random.Next(image.Height);                                       
  85.                     image.SetPixel(x, y, Color.FromArgb(random.Next()));                               
  86.                 }                   
  87.                 //把產(chǎn)生的隨機數(shù)以字體的形式寫入畫面                               
  88.                 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));                               
  89.                 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);                               
  90.                 g.DrawString(checkCode, font, brush, 2, 2);                                
  91.                 //畫圖片的邊框線                               
  92.                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);                                
  93.                 System.IO.MemoryStream ms = new System.IO.MemoryStream();                               
  94.                 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);                               
  95.                 Response.ClearContent();                               
  96.                 Response.ContentType = "image/Gif";                               
  97.                 Response.BinaryWrite(ms.ToArray());                        
  98.             }                        
  99.             finally                        
  100.             {                               
  101.                 g.Dispose();                               
  102.                 image.Dispose();                        
  103.             }                 
  104.         }  
  105.     }  
  106. }  
  107.  

ASP.NET1.1驗證碼主要函數(shù)分析

1、通過隨機函數(shù)(Random)先產(chǎn)生驗證碼組成元素(這里為五位) 并將其轉(zhuǎn)換為字符串(屬性為只讀),完成后寫入“Cookie”中去以供驗證時調(diào)用。

2、將驗證碼字符串寫入圖形:

(1)建立一個位圖文件確定長和寬:

  1. System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);   

(a)System.Drawing. [C#] Bitmap(int width,int height);

(b)double Math.Ceiling (double a):返回大于或等于指定數(shù)字的最小整數(shù)。

(2)畫圖片的背景噪音點(60個):

  1. for(int i=0; i< 60; i++)  {   
  2. int x = random.Next(image.Width);   
  3. int y = random.Next(image.Height);   
  4. image.SetPixel(x, y, Color.FromArgb(random.Next()));   
  5. }   

(a) public virtual int Next(int maxValue);返回一個小于所指定***值的非負(fù)隨機數(shù)。 參數(shù):maxValue-要生成的隨機數(shù)的上限。maxValue 必須大于或等于零。

(b) image.SetPixel(int x,int y,Color color); 參數(shù): x-要設(shè)置的像素的 x 坐標(biāo);y-要設(shè)置的像素的 y 坐標(biāo);color-Color 結(jié)構(gòu),它表示要分配給指定像素的顏色。

(c) Color.FromArgb(int argb) 參數(shù):argb-指定 32 位 ARGB 值的值。

(3)把產(chǎn)生的隨機數(shù)以字體的形式寫入位圖:Graphics.DrawString(string s,F(xiàn)ont font,Brush brush,float x,float y);

參數(shù):s-要繪制的字符串;font-Font 對象,它定義字符串的文本格式;

brush-Brush 對象,它確定所繪制文本的顏色和紋理;

x-所繪制文本的左上角的 x 坐標(biāo);

y-所繪制文本的左上角的 y 坐標(biāo)。(在指定位置并且用指定的 Brush 和 Font 對象繪制指定的文本字符串)

(4) 畫圖片的邊框線: public void DrawRectangle(Pen pen, int x, int y, int width, int height);繪制由坐標(biāo)對、寬度和高度指定的矩形。

參數(shù):pen-Pen 對象,它確定矩形的顏色、寬度和樣式;

x-要繪制的矩形的左上角的 x 坐標(biāo);

y-要繪制的矩形的左上角的 y 坐標(biāo);

width-要繪制的矩形的寬度;height-要繪制的矩形的高度。

(5) 將圖片以二進制流的方式輸出加上格式并可顯示出來。

以上就是asp.net1.1中驗證碼產(chǎn)生的原理及其應(yīng)用。

【編輯推薦】

  1. ASP.NET MVC jQuery刪除鏈接
  2. ASP.NET MVC框架中引入JQUERY JQRTE控件
  3. ASP.NET MVC 示例項目:Suteki.Shop
  4. ASP.NET MVC三層架構(gòu)實例
  5. ASP.NET MVC架構(gòu)中依賴性注入的概念
責(zé)任編輯:book05 來源: cnblogs
相關(guān)推薦

2009-07-29 14:59:26

asp.net1.1ASP.NET2.0

2009-07-29 15:17:42

驗證控件ASP.NET1.1ASP.NET2.0

2009-07-29 15:51:29

ASP.NET中執(zhí)行w

2009-07-29 11:46:22

asp.net1.1

2009-07-29 15:26:43

ASP.NET連接Or

2009-07-29 15:42:37

asp.net1.1開

2009-07-29 15:58:54

靜態(tài)文件處理ASP.NET

2009-07-29 11:29:16

ASP.NET1.1ASP.NET2.0

2009-07-29 15:06:16

asp.net1.1asp.net2.0

2009-11-26 10:48:59

PHP驗證碼

2009-08-05 13:09:17

ASP.NET應(yīng)用執(zhí)行

2013-06-19 10:19:59

2010-01-08 13:46:30

VB.NET中文驗證碼

2021-01-19 10:29:34

短信驗證碼密碼

2022-02-11 07:10:15

驗證碼

2020-11-16 07:28:53

驗證碼

2017-05-16 14:18:08

2011-11-02 12:43:33

2011-11-02 16:46:41

點贊
收藏

51CTO技術(shù)棧公眾號