程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> asp.net之生成驗證碼的辦法集錦(一)

asp.net之生成驗證碼的辦法集錦(一)

編輯:C#入門知識

asp.net之生成驗證碼的辦法集錦(一)。本站提示廣大學習愛好者:(asp.net之生成驗證碼的辦法集錦(一))文章只能為提供參考,不一定能成為您想要的結果。以下是asp.net之生成驗證碼的辦法集錦(一)正文


如今許多網站都有注冊登錄的頁面,為了更好的知足用戶體驗和網站的平安性,許多網站都采取靜態生成的圖形碼或許是附加碼停止驗證,上面把生成驗證碼的辦法給年夜家整頓以下.

完成驗證技巧就是在辦事器端生成一個隨機數,並將其保留在內存中,發送給閱讀器,並以圖片的情勢提交給用戶。之前在做項目進程中,完成了一個應用script停止用戶注冊及登錄的驗證碼時,發明有各類生成驗證碼的方法,上面重要是幾種分歧的生成驗證碼的方法:

1、繪制純數字的網站驗證碼

本實例完成的是數字驗證碼技巧,即隨機生成4位數字作為驗證碼。在開辟繪制會員登錄驗證模塊時可使用數字驗證碼技巧。

■設計進程

在一個新建的窗體CheckCode.aspx中編寫生成數字驗證碼的辦法:

private string RndNum()
 {
 int number;
 char code;
 string checkCode = String.Empty;
 System.Random random = new Random();
 for (int i = 0; i < 4; i++)
 {
 number = random.Next();
 if (number % 2 == 0)
 code = (char)('0' + (char)(number % 10));
 else
 code = (char)('A' + (char)(number % 26));
 checkCode += code.ToString();
 }
 Response.Cookies.Add(new HttpCookie("yzmcode", 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 < 25; 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 < 100; 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();
 }
 } 

然後在Page_Load中挪用CreateCheckCodeImage():
 

protected void Page_Load(object sender, EventArgs e)
 {
 this.CreateCheckCodeImage(RndNum()); 
}
那末在我們登錄的窗口中便可以經由過程簡略的控件:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/CheckCode.aspx" Width="50" Height="30"/><asp:Label
 ID="Label1" runat="server" Text="看不清晰?點擊圖片換一個" Height="16px" 
 Font-Size="Small" ForeColor="Red"></asp:Label>
 <br />
 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
在Button1_Click中對驗證碼輸出的准確性停止斷定:
protected void Button1_Click(object sender, EventArgs e)
{
 if (String.Compare(Request.Cookies["yzmcode"].Value, TextBox1.Text, true) != 0)
 {
 Response.Write("<script>alert('驗證碼毛病!')</script>");
 }
 else
 Response.Write("<script>alert('驗證碼准確!')</script>");
}

2、繪制數字與字母組合的網站驗證碼

和純數字的很類似,詳細差別在隨機生成字符串的辦法中

 

private string GenerateCheckCode()
 {
 int number;
 char code;
 string checkCode = String.Empty;
 Random random = new Random();
 for (int i = 0; i < 4; i++)
 {
 number = random.Next();
 if (number % 2 == 0)
 code = (char)('0' + (char)(number % 10));
 else
 code = (char)('A' + (char)(number % 26));
 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 < 2; 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.Black), x1, y1, x2, y2);
 }
 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
 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 < 100; 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();
 }
 } 

在登錄的窗口停止驗證碼輸出能否准確的斷定:

protected void Button1_Click(object sender, EventArgs e)
 {
 HttpCookie cookie = Request.Cookies["CheckCode"];
 if (cookie.Value == this.TextBox1.Text.Trim())
 {
 Response.Write("<script>alert('驗證碼准確!')</script>");
 }
 else
 {
 Response.Write("<script>alert('驗證碼毛病!')</script>"); 
 }
 }

那末有關於數字與字母組合的網站驗證碼就生成了。

3、四則運算式的驗證碼

其他的和數字和字母混雜生成的驗證碼類似,我就不寫了,詳細差別在隨機生成四則運算式的辦法中,以下:

 

private string GenerateCheckCode()
 {
 Random rd = new Random();
 int Results = 0;
 int Num1 = rd.Next(10);
 int Num2 = rd.Next(10);
 string Expressions = "";
 int F = (rd.Next(4) + 1);
 switch (F)
 {
 case 1:
 Results = Num1 + Num2;
 Expressions = Num1 + "+" + Num2;
 break;
 case 2:
 Results = Num1 - Num2;
 Expressions = Num1 + "-" + Num2;
 break;
 case 3:
 Results = Num1 * Num2;
 Expressions = Num1 + "*" + Num2;
 break;
 case 4:
 if (Num2 > 0)
 {
 Results = Convert.ToInt16(Num1 / Num2);
 Expressions = Num1 + "/" + Num2;
 }
 else
 {
 Results = Num1;
 Expressions = Num1 + "/1";
 }
 break;
 }
 Session["Code"] = Results.ToString();
 return Expressions;
}

此篇文章重要引見了三種辦法生成驗證碼,願望年夜家愛好。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved