成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

ASP.NET實現字母+數字方式驗證碼

2010-08-28 10:52:54來源:西部e網作者:

//ValidateCode.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ValidateCode
{
///
/// ValidateCode 的摘要說明。
///
public class ValidateCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
CreateCheckCodeImage(GenerateCheckCode());
}
//產生隨機數
private string GenerateCheckCode()
{
int number;
string strCode = string.Empty;
//隨機種子
Random random = new Random();
for (int i = 0; i < 4; i++) //驗證碼長度為4
{
//隨機整數
number = random.Next();
//字符從0-9,A-Z中產生,對應的ASCII碼為48-57,65-90
number = number % 36;
if (number < 10)
{
number += 48;
}
else
{
number += 55;
}
strCode += ((char)number).ToString();
}
//將字符串保存在Cookies
Response.Cookies.Add(new HttpCookie("CheckCode", strCode));
return strCode;
}

//生成圖片
private void CreateCheckCodeImage(string checkCode)
{
//若檢驗碼為空,則直接返回
if (checkCode == null    checkCode.Trim() == string.Empty)
return;
//根據驗證碼長度確定輸出圖片的長度
System.Drawing.Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 15)), 20);
//創建Graphics對象
System.Drawing.Graphics g= Graphics.FromImage(image);

//生成隨機數種子
Random random = new Random();
//清除圖片背景顏色
g.Clear(Color.White);
//畫背景圖片的噪音線10條
for (int i = 0; i < 10; i++)
{
//噪音線起點坐標(x1,y1),終點坐標(x2,y2)
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);
}
//輸出圖片中校驗碼的字體:12號Arial,粗斜體
Font font=new Font("Arial",12,FontStyle.Bold FontStyle.Italic);
//線性漸變畫刷
System.Drawing.Drawing2D.LinearGradientBrush brush=new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0,0,image.Width,image.Width),Color.Blue,Color.Purple,1.2f,true);
g.DrawString(checkCode,font,brush,2,2);
//畫圖片的前景噪點50個
for(int i=0;i<50;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.SaddleBrown),0,0,image.Width-1,image.Height-1);
//創建內存流用于輸出圖片
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//圖片格式指定為png
image.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
//清除緩沖區流中的所有輸出
Response.ClearContent();
//輸出流的Http Mime類型設置為"image/Png"
Response.ContentType="image/Png";
//輸出圖片的二進制流
Response.BinaryWrite(ms.ToArray());

//釋放Bitmap和Graphics對象
g.Dispose();
image.Dispose();
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

 

//在需要驗證的頁面加入以下代碼

private void cmdOk_Click(object sender, System.EventArgs e)
{
//判斷Cookie是否為空,若為空則說明瀏覽器禁用了Cookie
if(Request.Cookies["CheckCode"] ==null)
{
lblMessage.Text="您的瀏覽器禁用Cookie,請打開Cookie功能以使用本系統";
lblMessage.Visible=true;
return;
}
//判斷輸入的驗證碼是否正確
if(String.Compare(Request.Cookies["CheckCode"].Value,txtCode.Text,true)!=0)
{
lblMessage.Text="驗證碼錯誤,請輸入正確的驗證碼!";
lblMessage.Visible=true;
return;
}
else
{
Response.Write("輸入正確");
lblMessage.Visible=false;
}
}

關鍵詞:ASP.NET

贊助商鏈接:

主站蜘蛛池模板: 田林县| 胶州市| 福海县| 茶陵县| 洪雅县| 凤阳县| 华坪县| 台安县| 惠安县| 新田县| 富源县| 咸丰县| 汝城县| 万安县| 芜湖市| 久治县| 改则县| 武宁县| 万全县| 田东县| 历史| 讷河市| 鄂托克旗| 建水县| 苏尼特右旗| 新乐市| 广德县| 伊通| 涞源县| 延津县| 正安县| 聂拉木县| 扎鲁特旗| 大厂| 汽车| 秦皇岛市| 同德县| 吉安市| 炉霍县| 宁国市| 隆林|