深入學(xué)習(xí).net驗(yàn)證碼生成及使用方法
小課堂:驗(yàn)證碼的作用:
幾年前,大部分網(wǎng)站、論壇之類(lèi)的是沒(méi)有驗(yàn)證碼的,因?yàn)閷?duì)于一般用戶(hù)來(lái)說(shuō)驗(yàn)證碼只是增加了用戶(hù)的操作,降低了用戶(hù)的體驗(yàn)。但是后來(lái)各種灌水機(jī)器人、投票機(jī)器人、惡意注冊(cè)機(jī)器人層出不窮,大大增加了網(wǎng)站的負(fù)擔(dān)同時(shí)也給網(wǎng)站數(shù)據(jù)庫(kù)帶來(lái)了大量的垃圾數(shù)據(jù)。為了防止各種機(jī)器人程序的破壞,于是程序員想出了只有人眼能夠識(shí)別的,程序不容易識(shí)別的驗(yàn)證碼!
驗(yàn)證碼是一個(gè)圖片,將字母、數(shù)字甚至漢字作為圖片的內(nèi)容,這樣一張圖片中的內(nèi)容用人眼很容易識(shí)別,而程序?qū)o(wú)法識(shí)別。在進(jìn)行數(shù)據(jù)庫(kù)操作之前(比如登錄驗(yàn)證、投票、發(fā)帖、回復(fù)、注冊(cè)等等)程序首先驗(yàn)證客戶(hù)端提交的驗(yàn)證碼是否與圖片中的內(nèi)容相同,如果相同則進(jìn)行數(shù)據(jù)庫(kù)操作,不同則提示驗(yàn)證碼錯(cuò)誤,不進(jìn)行數(shù)據(jù)庫(kù)操作。這樣各種機(jī)器人程序就被拒之門(mén)外了!
但是隨著計(jì)算機(jī)科學(xué)的發(fā)展,模式識(shí)別等技術(shù)越來(lái)越成熟,于是編寫(xiě)機(jī)器人程序的家伙可以通過(guò)程序?qū)⒅苯訉?xiě)在圖片中的內(nèi)容識(shí)別出來(lái),然后提交到服務(wù)器,這樣驗(yàn)證碼將形同虛設(shè)。為了防止機(jī)器人程序的識(shí)別,驗(yàn)證碼的圖片生成也不斷在發(fā)展,加入干擾點(diǎn)、干擾線,文字變形、變換角度位置,顏色不同……各種防止計(jì)算機(jī)識(shí)別的技術(shù)也應(yīng)用到驗(yàn)證碼中。就在這兩種技術(shù)的競(jìng)爭(zhēng)中,于是便形成了我們現(xiàn)在看到的驗(yàn)證碼,已經(jīng)有很多人在抱怨“這是什么驗(yàn)證碼哦,人眼都分辨不清楚是什么”,一切也是無(wú)奈。
了解了驗(yàn)證碼的作用,下面寫(xiě)一個(gè)簡(jiǎn)單的驗(yàn)證碼生成及使用的實(shí)例

先建個(gè)頁(yè)面用來(lái)展示驗(yàn)證碼和判斷驗(yàn)證碼輸入是否正確
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
//點(diǎn)擊切換驗(yàn)證碼
function f_refreshtype() {
var Image1 = document.getElementById("img");
if (Image1 != null) {
Image1.src = Image1.src + "?";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<img src="png.aspx" id="img" onclick="f_refreshtype()" />
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="確定" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
此頁(yè)面后臺(tái)對(duì)驗(yàn)證碼進(jìn)行驗(yàn)證
protected void Page_Load(object sender, EventArgs e)
{
//生成的驗(yàn)證碼被保存到session中
if (Session["CheckCode"] != null)
{
string checkcode = Session["CheckCode"].ToString();
if (this.TextBox1.Text == checkcode)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('驗(yàn)證碼輸入正確!')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('驗(yàn)證碼輸入錯(cuò)誤!')", true);
}
}
}
生成驗(yàn)證碼頁(yè)面png.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateCheckCodeImage(GenerateCheckCodes(4));
}
}
public void ShowAuthCode(Stream stream, out string code)
{
Random random = new Random();
code = random.Next(1000, 9999).ToString();
Bitmap bitmap = CreateAuthCode(code);
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
}
private string GenerateCheckCodes(int iCount)
{
int number;
string checkCode = String.Empty;
int iSeed = DateTime.Now.Millisecond;
System.Random random = new Random(iSeed);
for (int i = 0; i < iCount; i++)
{
number = random.Next(10);
checkCode += number.ToString();
}
Session["CheckCode"] = checkCode;
return checkCode;
}
private Bitmap CreateAuthCode(string str)
{
Font fn = new Font("宋體", 12);
Brush forecolor = Brushes.Black;
Brush bgcolor = Brushes.White;
PointF pf = new PointF(5, 5);
Bitmap bitmap = new Bitmap(100, 25);
Rectangle rec = new Rectangle(0, 0, 100, 25);
Graphics gh = Graphics.FromImage(bitmap);
gh.FillRectangle(bgcolor, rec);
gh.DrawString(str, fn, forecolor, pf);
return bitmap;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
int iWordWidth = 15;
int iImageWidth = checkCode.Length * iWordWidth;
Bitmap image = new Bitmap(iImageWidth, 20);
Graphics g = Graphics.FromImage(image);
try
{
//生成隨機(jī)生成器
Random random = new Random();
//清空?qǐng)D片背景色
g.Clear(Color.White);
//畫(huà)圖片的背景噪音點(diǎn)
for (int i = 0; i < 20; 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);
}
//畫(huà)圖片的背景噪音線
for (int i = 0; i < 2; i++)
{
int x1 = 0;
int x2 = image.Width;
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
if (i == 0)
{
g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2);
}
}
for (int i = 0; i < checkCode.Length; i++)
{
string Code = checkCode[i].ToString();
int xLeft = iWordWidth * (i);
random = new Random(xLeft);
int iSeed = DateTime.Now.Millisecond;
int iValue = random.Next(iSeed) % 4;
if (iValue == 0)
{
Font font = new Font("Arial", 13, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 1)
{
Font font = new System.Drawing.Font("楷體", 13, (FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 2)
{
Font font = new System.Drawing.Font("宋體", 13, (System.Drawing.FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
else if (iValue == 3)
{
Font font = new System.Drawing.Font("黑體", 13, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold));
Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true);
g.DrawString(Code, font, brush, xLeft, 2);
}
}
//////畫(huà)圖片的前景噪音點(diǎn)
//for (int i = 0; i < 8; i++)
//{
// int x = random.Next(image.Width);
// int y = random.Next(image.Height);
// image.SetPixel(x, y, Color.FromArgb(random.Next()));
//}
//畫(huà)圖片的邊框線
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.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
- asp.net登錄驗(yàn)證碼實(shí)現(xiàn)方法
- 詳解ASP.NET驗(yàn)證碼的生成方法
- asp.net生成字母和數(shù)字混合圖形驗(yàn)證碼
- asp.net簡(jiǎn)單生成驗(yàn)證碼的方法
- ASP.NET驗(yàn)證碼實(shí)現(xià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
相關(guān)文章
.net core利用orm如何操作mysql數(shù)據(jù)庫(kù)詳解
這篇文章主要給大家介紹了關(guān)于.net core利用orm如何操作mysql數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
封裝的一個(gè)asp.net驗(yàn)證碼類(lèi)
昨天在一個(gè)Q群上面群主發(fā)了一個(gè)用ASP.NET實(shí)現(xiàn)驗(yàn)證碼的demo,下載下來(lái)然后運(yùn)行正常,頁(yè)面上的img標(biāo)簽成功調(diào)用了一個(gè)一般處理程序并顯示了中文的驗(yàn)證碼圖片,雖然有點(diǎn)模糊,但是可見(jiàn)上面是四個(gè)中文,圖片背景為白色,背后有噪點(diǎn)線,邊框黑色。2010-12-12
asp.net微信開(kāi)發(fā)(用戶(hù)分組管理)
這篇文章主要介紹了asp.net微信開(kāi)發(fā)中有關(guān)用戶(hù)分組管理的相關(guān)內(nèi)容,需要的朋友可以參考下2015-11-11
asp.net實(shí)現(xiàn)調(diào)用存儲(chǔ)過(guò)程并帶返回值的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)調(diào)用存儲(chǔ)過(guò)程并帶返回值的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了asp.net存儲(chǔ)過(guò)程調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-03-03
微信公眾平臺(tái)開(kāi)發(fā)之處理圖片.Net代碼解析
這篇文章主要為大家詳細(xì)解析了微信公眾平臺(tái)開(kāi)發(fā)之處理圖片.Net代碼,感興趣的小伙伴們可以參考一下2016-06-06
如何在網(wǎng)站級(jí)別動(dòng)態(tài)更改主題
如何在網(wǎng)站級(jí)別動(dòng)態(tài)更改主題...2007-04-04
NET Core 3.0 AutoFac內(nèi)置DI替換的新姿勢(shì)分享
這篇文章主要給大家介紹了關(guān)于NET Core 3.0 AutoFac內(nèi)置DI替換的新姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用NET Core 3.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
ASP.NET調(diào)用WebService服務(wù)的方法詳解
這篇文章主要介紹了ASP.NET調(diào)用WebService服務(wù)的方法,較為詳細(xì)的分析了WebService服務(wù)的功能,創(chuàng)建步驟與使用方法,需要的朋友可以參考下2016-05-05

