一個(gè)簡(jiǎn)單的ASP.NET驗(yàn)證碼
本文實(shí)例為大家分享了ASP.NET驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下
我主要是看到干擾線了,一個(gè)驗(yàn)證碼里面要是沒(méi)有干擾線什么的,至少得在噪點(diǎn)和隨機(jī)碼的排版上下工夫:
/// <summary>
/// 驗(yàn)證碼生成類(lèi)
/// </summary>
public class verify_code : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
int codeW = 80;
int codeH = 22;
int fontSize = 16;
string chkCode = string.Empty;
//顏色列表,用于驗(yàn)證碼、噪線、噪點(diǎn)
Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
//字體列表,用于驗(yàn)證碼
string[] font = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
//驗(yàn)證碼的字符集,去掉了一些容易混淆的字符
char[] character = { '0', '1', '2', '3', '4', '5', '6', '8', '9' };
Random rnd = new Random();
//生成驗(yàn)證碼字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//寫(xiě)入Session
context.Session["sys_verify_code"] = chkCode;
//創(chuàng)建畫(huà)布
Bitmap bmp = new Bitmap(codeW, codeH);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//畫(huà)噪線
for (int i = 0; i < 4; i++)
{
int x1 = rnd.Next(codeW);
int y1 = rnd.Next(codeH);
int x2 = rnd.Next(codeW);
int y2 = rnd.Next(codeH);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//畫(huà)驗(yàn)證碼字符串
for (int i = 0; i < chkCode.Length; i++)
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, fontSize);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18 + 2, (float)0);
}
//畫(huà)噪點(diǎn)
for (int i = 0; i < 100; i++)
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//清除該頁(yè)輸出緩存,設(shè)置該頁(yè)無(wú)緩存
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
context.Response.Expires = 0;
context.Response.CacheControl = "no-cache";
context.Response.AppendHeader("Pragma", "No-Cache");
//將驗(yàn)證碼圖片寫(xiě)入內(nèi)存流,并將其以 "image/Png" 格式輸出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
context.Response.ClearContent();
context.Response.ContentType = "image/Png";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
//顯式釋放資源
bmp.Dispose();
g.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
基本驗(yàn)證生成代碼demo:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public partial class image : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string tmp = RndNum(4);
HttpCookie a = new HttpCookie("ImageV", tmp);
Response.Cookies.Add(a);
this.ValidateCode(tmp);
}
private void ValidateCode(string VNum)
{
Bitmap Img = null;
Graphics g = null;
MemoryStream ms = null;
int gheight = VNum.Length * 12;
Img = new Bitmap(gheight, 25);
g = Graphics.FromImage(Img);
//背景顏色
g.Clear(Color.White);
//文字字體
Font f = new Font("Arial Black", 10);
//文字顏色
SolidBrush s = new SolidBrush(Color.Black);
g.DrawString(VNum, f, s, 3, 3);
ms = new MemoryStream();
Img.Save(ms, ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
Img.Dispose();
Response.End();
}
private string RndNum(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +
",q,r,s,t,u,v,w,x,y,z";
string[] VcArray = Vchar.Split(new Char[] { ',' });
string VNum = "";
int temp = -1;
Random rand = new Random();
for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);
}
temp = t;
VNum += VcArray[t];
}
return VNum;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
支持Ajax跨域訪問(wèn)ASP.NET Web Api 2(Cors)的示例教程
這篇文章主要介紹了支持Ajax跨域訪問(wèn)ASP.NET Web Api 2(Cors)的示例教程,需要的朋友可以參考下2016-04-04
asp.net獲得數(shù)據(jù)控件事件索引并獲取其中值總結(jié)
asp.net獲得數(shù)據(jù)控件事件索引并獲取其中值總結(jié),需要的朋友可以參考下。2011-11-11
asp.net 獲取Datalist中Checkbox的值的小結(jié)
最近開(kāi)發(fā)過(guò)程中遇到一個(gè)小問(wèn)題,要獲取checkbox的值,在網(wǎng)上搜索了一下,發(fā)現(xiàn)基本上都是用JS實(shí)現(xiàn)的,現(xiàn)在我將自己的做法記錄一下,以便以后繼續(xù)使用。2010-04-04
asp.net?core實(shí)體類(lèi)生產(chǎn)CRUD后臺(tái)管理界面
這篇文章主要為大家介紹了asp.net?core實(shí)體類(lèi)生產(chǎn)CRUD后臺(tái)管理界面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
ASP.NET頁(yè)面間數(shù)據(jù)傳遞的幾種方法介紹
在ASP.NET中,頁(yè)面間數(shù)據(jù)傳遞的方法有很多。下面為大家總結(jié)一下,頁(yè)面間數(shù)據(jù)傳遞的方法,來(lái)看作者的分析。2013-05-05
asp.net EncryptHelper 加密幫助類(lèi)
EncryptHelper 加密幫助類(lèi)2010-01-01
ASP.NET MVC驗(yàn)證碼功能實(shí)現(xiàn)代碼
ASP.NET MVC驗(yàn)證碼功能實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-06-06
web.config中配置數(shù)據(jù)庫(kù)連接的方式
Web.config文件是一個(gè)XML文本文件,它用來(lái)儲(chǔ)存 ASP.NET Web 應(yīng)用程序的配置信息(如最常用的設(shè)置ASP.NET Web 應(yīng)用程序的身份驗(yàn)證方式),它可以出現(xiàn)在應(yīng)用程序的每一個(gè)目錄中。本文主要介紹web.config中配置數(shù)據(jù)庫(kù)連接的兩種方式,一起來(lái)看。2015-10-10

