一般驗(yàn)證碼的生成方法都是相同的,主要的步驟都有兩步
第一步:隨機(jī)出一系統(tǒng)驗(yàn)證碼的數(shù)字或字母,順便把隨機(jī)生成的數(shù)字或字母寫(xiě)入Cookies 或者 Session。
第二步:用第一步隨機(jī)出來(lái)的數(shù)字或字母來(lái)合成圖片。
可以看出來(lái)驗(yàn)證碼的復(fù)雜度主要是第二步來(lái)完成,你可以根據(jù)自己所要的復(fù)雜度來(lái)設(shè)定。
我們一起來(lái)看看:
第一步:隨機(jī)生成數(shù)字或字母的方法
/// summary>
/// 生成驗(yàn)證碼的隨機(jī)數(shù)
/// /summary>
/// returns>返回五位隨機(jī)數(shù)/returns>
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
Random random = new Random();
for (int i = 0; i 5; i++)//可以任意設(shè)定生成驗(yàn)證碼的位數(shù)
{
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));//寫(xiě)入COOKIS
Session["CheckCode"] = checkCode; //寫(xiě)入Session,可以任意選一下
return checkCode;
}
第二步:生成圖片
/// summary>
/// 生成驗(yàn)證碼圖片
/// /summary>
/// param name="checkCode">/param>
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機(jī)生成器
Random random = new Random();
//清空?qǐng)D片背景色
g.Clear(Color.White);
//畫(huà)圖片的背景噪音線(xiàn)
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));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//畫(huà)圖片的前景噪音點(diǎn)
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()));
}
//畫(huà)圖片的邊框線(xiàn)
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{//釋放對(duì)象資源
g.Dispose();
image.Dispose();
}
*完整程序
先在VS2005里面的項(xiàng)目里面添加一個(gè) checkCode.aspx 文件,在checkCode.aspx.cs 代碼文件中添加如下完整代碼
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
public partial class checkCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(GenerateCheckCode());//調(diào)用下面兩個(gè)方法;
}
/// summary>
/// 生成驗(yàn)證碼的隨機(jī)數(shù)
/// /summary>
/// returns>返回五位隨機(jī)數(shù)/returns>
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
Random random = new Random();
for (int i = 0; i 5; i++)//可以任意設(shè)定生成驗(yàn)證碼的位數(shù)
{
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));//寫(xiě)入COOKIS
Session["CheckCode"] = checkCode; //寫(xiě)入Session,可以任意選一下
return checkCode;
}
/// summary>
/// 生成驗(yàn)證碼圖片
/// /summary>
/// param name="checkCode">/param>
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機(jī)生成器
Random random = new Random();
//清空?qǐng)D片背景色
g.Clear(Color.White);
//畫(huà)圖片的背景噪音線(xiàn)
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));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//畫(huà)圖片的前景噪音點(diǎn)
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()));
}
//畫(huà)圖片的邊框線(xiàn)
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{//釋放對(duì)象資源
g.Dispose();
image.Dispose();
}
}
}
上面生成驗(yàn)證碼的頁(yè)面都做好了,我們來(lái)調(diào)用看看:
在你需要用到驗(yàn)證碼的地方加了Image控件
asp:Image ID="Image1" runat="server" ImageUrl="~/checkCode.aspx" />
這樣驗(yàn)證碼就會(huì)顯示到Image控件上面了!
顯示弄好了,當(dāng)然我們要判斷一下用戶(hù)的輸入是否正確!
只要我們?nèi)〉糜脩?hù)輸入的值跟Cookis或者Session對(duì)比就OK了
取Cookies的值 Request.Cookies["CheckCode"].Value
取Session的值 Session["CheckCode"].ToString() ?。ㄗ詈孟扰袛郤ession是否空)
如果不要區(qū)分大小寫(xiě)的話(huà),就把用戶(hù)輸入的值和Cookies或Session的值都轉(zhuǎn)成大寫(xiě)或都小寫(xiě)
附用法
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies["CheckCode"].Value == TextBox1.Text.Trim().ToString())
{
Response.Write("Cookies is right");
}
else
{
Response.Write("Cookies is wrong");
}
if (Session["CheckCode"] != null)
{
if (Session["CheckCode"].ToString().ToUpper() == TextBox1.Text.Trim().ToString().ToUpper())
//這樣寫(xiě)可以不能區(qū)分大小寫(xiě)
{
Response.Write("Session is right");
}
else
{
Response.Write("Session is wrong");
}
}
}
以上就是本文的全部?jī)?nèi)容,教大家如何制作ASP.NET驗(yàn)證碼,希望大家喜歡。
您可能感興趣的文章:- asp.net登錄驗(yàn)證碼實(shí)現(xiàn)方法
- asp.net生成字母和數(shù)字混合圖形驗(yàn)證碼
- asp.net簡(jiǎn)單生成驗(yàn)證碼的方法
- ASP.NET驗(yàn)證碼實(shí)現(xiàn)(附源碼)
- 深入學(xué)習(xí).net驗(yàn)證碼生成及使用方法
- .net生成驗(yàn)證碼
- asp.net驗(yàn)證碼的簡(jiǎn)單制作
- 12306動(dòng)態(tài)驗(yàn)證碼啟發(fā)之ASP.NET實(shí)現(xiàn)動(dòng)態(tài)GIF驗(yàn)證碼(附源碼)
- ASP.NET驗(yàn)證碼(3種)
- MVC使用極驗(yàn)驗(yàn)證制作登錄驗(yàn)證碼學(xué)習(xí)筆記7