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