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

asp.net生成字母和数字混合图形验证码

程序员文章站 2023-12-20 18:26:04
验证码技术是网站开发过程中比较重要的技术,可以防止非法人员利用注册机或者登陆工具来攻击我们的网站。下面是效果图: 具体实现方法如下: 1、主要思路是:引用using...

验证码技术是网站开发过程中比较重要的技术,可以防止非法人员利用注册机或者登陆工具来攻击我们的网站。下面是效果图:

asp.net生成字母和数字混合图形验证码

具体实现方法如下:

1、主要思路是:引用using system.drawing命名空间,利用graphics的fromimage方法创建一个画布,同时设置画布的宽和高,然后通过graphics类 的drawstring方法随机生成的字符串绘制到画布中,绘制验证码的同时,在画布中利用setpixel方法绘制一些色点,从而防止非法人员利用机器 人来进行登陆。当我们绘制验证码完毕后,在需要验证码的页面中利用image空间将其显示出来,image控件显示验证码的html源码设置如下:

<asp:image id="image1" runat="server"imageurl="~/validatenum.aspx" />
<asp:linkbutton id="linkbutton1" runat="server" style="font-size: small; ">看不清,换一张</asp:linkbutton>

2、这里所用到的validatenum.aspx页面代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.drawing;

public partial class validatenum : system.web.ui.page
{
  protected void page_load(object sender, eventargs e)
  {
    if (!ispostback)
    {
      string validatenum = createrandomnum(4);//成生4位随机字符串
      createimage(validatenum);//将生成的随机字符串绘成图片
      session["validatenum"] = validatenum;//保存验证码
    }
  }
  //生成随机字符串
  private string createrandomnum(int numcount)
  {
    string allchar = "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,w,x,y,z";
    string[] allchararray = allchar.split(',');//拆分成数组
    string randomnum = "";
    int temp = -1;//记录上次随机数的数值,尽量避免产生几个相同的随机数

    random rand = new random();
    for (int i = 0; i < numcount; i++)
    {
      if (temp != -1)
      {
        rand = new random(i * temp * ((int)datetime.now.ticks));
      }
      int t = rand.next(35);
      if (temp == t)
      {
        return createrandomnum(numcount);
      }
      temp = t;
      randomnum += allchararray[t];
    }
    return randomnum;
  }
  //生成图片
  private void createimage(string validatenum)
  {
    if (validatenum == null || validatenum.trim() == string.empty)
      return;
    //生成bitmap图像
    system.drawing.bitmap image = new system.drawing.bitmap(validatenum.length * 12 + 10, 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(validatenum, 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();
    }
  }
}

以上就是关于asp.net生成图形验证码的相关资料,希望对大家的学习有所帮助。

上一篇:

下一篇: