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

一个简单的ASP.NET验证码

程序员文章站 2022-06-21 08:38:41
本文实例为大家分享了asp.net验证码的具体代码,供大家参考,具体内容如下 我主要是看到干扰线了,一个验证码里面要是没有干扰线什么的,至少得在噪点和随机码的排版上下工夫...

本文实例为大家分享了asp.net验证码的具体代码,供大家参考,具体内容如下

我主要是看到干扰线了,一个验证码里面要是没有干扰线什么的,至少得在噪点和随机码的排版上下工夫:

 /// <summary>
 /// 验证码生成类
 /// </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;
   //颜色列表,用于验证码、噪线、噪点 
   color[] color = { color.black, color.red, color.blue, color.green, color.orange, color.brown, color.brown, color.darkblue };
   //字体列表,用于验证码 
   string[] font = { "times new roman", "verdana", "arial", "gungsuh", "impact" };
   //验证码的字符集,去掉了一些容易混淆的字符 
   char[] character = { '0', '1', '2', '3', '4', '5', '6', '8', '9' };
   random rnd = new random();
   //生成验证码字符串 
   for (int i = 0; i < 4; i++)
   {
    chkcode += character[rnd.next(character.length)];
   }
   //写入session
   context.session["sys_verify_code"] = chkcode;
   //创建画布
   bitmap bmp = new bitmap(codew, codeh);
   graphics g = graphics.fromimage(bmp);
   g.clear(color.white);
   //画噪线 
   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);
   }
   //画验证码字符串 
   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);
   }
   //画噪点 
   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);
   }
   //清除该页输出缓存,设置该页无缓存 
   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");
   //将验证码图片写入内存流,并将其以 "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;
   }
  }
 }

基本验证生成代码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;
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。