ASP.net 驗證碼實現(xiàn)代碼(C#)
更新時間:2008年02月25日 14:39:55 作者:
asp.net 驗證碼效果實現(xiàn)代碼
public class ValidateCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for(int i=0; i<5; 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<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.C;
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
假如以上驗證碼生成器頁面名為:CheckCode.aspx,那么在登錄頁面中使用“<IMG>” 這個 HTML 元素來顯示生成的驗證碼圖片:<IMG src="CheckCode.aspx">
在登錄頁面的登錄按鈕的處理事件中使用以下代碼判斷驗證碼:
private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Request.Cookies["CheckCode"] == null)
{
lblMessage.Text = "您的瀏覽器設置已被禁用 Cookies,您必須設置瀏覽器允許使用 Cookies 選項后才能使用本系統(tǒng)。";
lblMessage.Visible = true;
return;
}
if(String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true) != 0)
{
lblMessage.Text = "驗證碼錯誤,請輸入正確的驗證碼。";
lblMessage.Visible = true;
return;
}
}
{
private void Page_Load(object sender, System.EventArgs e)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for(int i=0; i<5; 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<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.C;
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
假如以上驗證碼生成器頁面名為:CheckCode.aspx,那么在登錄頁面中使用“<IMG>” 這個 HTML 元素來顯示生成的驗證碼圖片:<IMG src="CheckCode.aspx">
在登錄頁面的登錄按鈕的處理事件中使用以下代碼判斷驗證碼:
private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Request.Cookies["CheckCode"] == null)
{
lblMessage.Text = "您的瀏覽器設置已被禁用 Cookies,您必須設置瀏覽器允許使用 Cookies 選項后才能使用本系統(tǒng)。";
lblMessage.Visible = true;
return;
}
if(String.Compare(Request.Cookies["CheckCode"].Value, txtCheckCode.Text, true) != 0)
{
lblMessage.Text = "驗證碼錯誤,請輸入正確的驗證碼。";
lblMessage.Visible = true;
return;
}
}
相關文章
使用EF的Code?First模式操作數(shù)據(jù)庫
這篇文章介紹了使用EF的Code?First模式操作數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03CheckBox為CheckBoxList實現(xiàn)全選或全取消選擇(js代碼實現(xiàn))
在管理商品后臺是,由于CheckBoxList的選擇太多,用戶需要一個全選或全取消的功能,這樣操作起來會提高效率同時可以減少誤點等,本文將教大家如何實現(xiàn),有需要的朋友可以參考下,望本文對你有所幫助2013-01-01.NET從優(yōu)酷專輯中采集所有視頻及信息(VB.NET代碼)
因為想做一個視頻點播類的網(wǎng)站,所以開始研究視頻采集。2010-02-02利用sender的Parent獲取GridView中的當前行(獲取gridview的值)
這篇文章主要介紹了利用sender的Parent獲取GridView中的當前行的方法,大家參考使用吧2014-01-01.Net Core導入千萬級數(shù)據(jù)至Mysql數(shù)據(jù)庫的實現(xiàn)方法
今天我們談談MySQL怎么高性能插入千萬級的數(shù)據(jù)的,討論這個問題牽扯到一個數(shù)據(jù)遷移功能,高性能的插入數(shù)據(jù),接下來通過本文給大家分享幾種實現(xiàn)方法,感興趣的朋友跟隨小編一起學習下吧2021-05-05