欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net之生成验证码的方法集锦(一)

程序员文章站 2023-11-16 23:25:46
现在很多网站都有注册登录的页面,为了更好的满足用户体验和网站的安全性,很多网站都采用动态生成的图形码或者是附加码进行验证,下面把生成验证码的方法给大家整理如下. 实现验证...

现在很多网站都有注册登录的页面,为了更好的满足用户体验和网站的安全性,很多网站都采用动态生成的图形码或者是附加码进行验证,下面把生成验证码的方法给大家整理如下.

实现验证技术就是在服务器端生成一个随机数,并将其保存在内存中,发送给浏览器,并以图片的形式提交给用户。之前在做项目过程中,完成了一个利用script进行用户注册及登录的验证码时,发现有各种生成验证码的方式,下面主要是几种不同的生成验证码的方式:

1、绘制纯数字的网站验证码

本实例实现的是数字验证码技术,即随机生成4位数字作为验证码。在开发绘制会员登录验证模块时可以使用数字验证码技术。

■设计过程

在一个新建的窗体checkcode.aspx中编写生成数字验证码的方法:

private string rndnum()
 {
 int number;
 char code;
 string checkcode = string.empty;
 system.random random = new random();
 for (int i = 0; i < 4; 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("yzmcode", 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.contenttype = "image/gif";
 response.binarywrite(ms.toarray());
 }
 finally
 {
 g.dispose();
 image.dispose();
 }
 } 

然后在page_load中调用createcheckcodeimage():
 

protected void page_load(object sender, eventargs e)
 {
 this.createcheckcodeimage(rndnum()); 
}
那么在我们登录的窗口中就可以通过简单的控件:
<asp:textbox id="textbox1" runat="server"></asp:textbox>
 <asp:imagebutton id="imagebutton2" runat="server" imageurl="~/checkcode.aspx" width="50" height="30"/><asp:label
 id="label1" runat="server" text="看不清楚?点击图片换一个" height="16px" 
 font-size="small" forecolor="red"></asp:label>
 <br />
 <asp:button id="button1" runat="server" onclick="button1_click" text="button" />
在button1_click中对验证码输入的正确性进行判断:
protected void button1_click(object sender, eventargs e)
{
 if (string.compare(request.cookies["yzmcode"].value, textbox1.text, true) != 0)
 {
 response.write("<script>alert('验证码错误!')</script>");
 }
 else
 response.write("<script>alert('验证码正确!')</script>");
}

2、绘制数字与字母组合的网站验证码

和纯数字的很相似,具体区别在随机生成字符串的方法中

 

private string generatecheckcode()
 {
 int number;
 char code;
 string checkcode = string.empty;
 random random = new random();
 for (int i = 0; i < 4; 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 < 2; 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.black), x1, y1, x2, y2);
 }
 font font = new system.drawing.font("arial", 12, (system.drawing.fontstyle.bold));
 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.contenttype = "image/gif";
 response.binarywrite(ms.toarray());
 }
 finally
 {
 g.dispose();
 image.dispose();
 }
 } 

在登录的窗口进行验证码输入是否正确的判断:

protected void button1_click(object sender, eventargs e)
 {
 httpcookie cookie = request.cookies["checkcode"];
 if (cookie.value == this.textbox1.text.trim())
 {
 response.write("<script>alert('验证码正确!')</script>");
 }
 else
 {
 response.write("<script>alert('验证码错误!')</script>"); 
 }
 }

那么有关于数字与字母组合的网站验证码就生成了。

3、四则运算式的验证码

其他的和数字和字母混合生成的验证码相似,我就不写了,具体区别在随机生成四则运算式的方法中,如下:

 

private string generatecheckcode()
 {
 random rd = new random();
 int results = 0;
 int num1 = rd.next(10);
 int num2 = rd.next(10);
 string expressions = "";
 int f = (rd.next(4) + 1);
 switch (f)
 {
 case 1:
 results = num1 + num2;
 expressions = num1 + "+" + num2;
 break;
 case 2:
 results = num1 - num2;
 expressions = num1 + "-" + num2;
 break;
 case 3:
 results = num1 * num2;
 expressions = num1 + "*" + num2;
 break;
 case 4:
 if (num2 > 0)
 {
 results = convert.toint16(num1 / num2);
 expressions = num1 + "/" + num2;
 }
 else
 {
 results = num1;
 expressions = num1 + "/1";
 }
 break;
 }
 session["code"] = results.tostring();
 return expressions;
}

此篇文章主要介绍了三种方法生成验证码,希望大家喜欢。