当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net生成验证码(纯数字)

asp.net生成验证码(纯数字)

2017年12月12日  | 移动技术网IT编程  | 我要评论

windows764旗舰版,巨星成长计划2.6,课件下载网

checkcode.cs
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.drawing;
/// <summary>
/// checkcode 的摘要说明
/// </summary>
public class checkcode
{
public checkcode()
{
//
// todo: 在此处添加构造函数逻辑
//
}
public static void drawimage()
{
checkcode img = new checkcode();
httpcontext.current.session["checkcode"] = img.rndnum(4);
img.checkcodes(httpcontext.current.session["checkcode"].tostring());
}
/// <summary>
/// 生成验证图片
/// </summary>
/// <param name="checkcode">验证字符</param>
private void checkcodes(string checkcode)
{
int iwidth = (int)(checkcode.length * 13);
system.drawing.bitmap image = new system.drawing.bitmap(iwidth, 23);
graphics g = graphics.fromimage(image);
g.clear(color.white);
//定义颜色
color[] c = { color.black, color.red, color.darkblue, color.green, color.orange, color.brown, color.darkcyan, color.purple };
//定义字体
string[] font = { "verdana", "microsoft sans serif", "comic sans ms", "arial", "宋体" };
random rand = new random();
//随机输出噪点
for (int i = 0; i < 50; i++)
{
int x = rand.next(image.width);
int y = rand.next(image.height);
g.drawrectangle(new pen(color.lightgray, 0), x, y, 1, 1);
}
//输出不同字体和颜色的验证码字符
for (int i = 0; i < checkcode.length; i++)
{
int cindex = rand.next(7);
int findex = rand.next(5);
font f = new system.drawing.font(font[findex], 10, system.drawing.fontstyle.bold);
brush b = new system.drawing.solidbrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.drawstring(checkcode.substring(i, 1), f, b, 3 + (i * 12), ii);
}
//画一个边框
g.drawrectangle(new pen(color.black, 0), 0, 0, image.width - 1, image.height - 1);
//输出到浏览器
system.io.memorystream ms = new system.io.memorystream();
image.save(ms, system.drawing.imaging.imageformat.jpeg);
httpcontext.current.response.clearcontent();
//response.clearcontent();
httpcontext.current.response.contenttype = "image/jpeg";
httpcontext.current.response.binarywrite(ms.toarray());
g.dispose();
image.dispose();
}
/// <summary>
/// 生成随机的字母
/// </summary>
/// <param name="vcodenum">生成字母的个数</param>
/// <returns>string</returns>
private string rndnum(int vcodenum)
{
string vchar = "0,1,2,3,4,5,6,7,8,9";
string[] vcarray = vchar.split(',');
string vnum = ""; //由于字符串很短,就不用stringbuilder了
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(vcarray.length);
if (temp != -1 && temp == t)
{
return rndnum(vcodenum);
}
temp = t;
vnum += vcarray[t];
}
return vnum;
}
}

再建立一个引用类的页面checkcode.aspx前台不用写东西,后台引用我们创建的类的drawimage()方法即可。
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class checkcode : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
checkcode.drawimage();
}
}

下面我们在需要验证码的页面引用checkcode.aspx页面即可。
前台
复制代码 代码如下:

<asp:textbox id="validator" runat="server" width="150px" ></asp:textbox>
<img id="img1" alt="看不清,请点击我!" onclick="this.src=this.src+'?'" src="checkcode.aspx"
style="width: 73px; height: 22px" align="left" />
<asp:imagebutton id="imgbtnlogin" runat="server" imageurl="~/images/login.gif"
onclick="imgbtnlogin_click" />

后台判断
复制代码 代码如下:

protected void imgbtnlogin_click(object sender, imageclickeventargs e)
{
if(this.validator.text==session["checkcode"].tostring())
{
//。。。。
}
else
{
response.write("<script>alert('验证码输入错误,请重新输入!');location='mumbervalidate.aspx'</script>");
return;
}
}

以上代码请根据实际情况作适当修改。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网