当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET验证码实现(附源码)

ASP.NET验证码实现(附源码)

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

个人房屋出租协议,电脑报合订本,长生不死燃文

首先看下效果实现(由于gif屏幕录制软件是即时找的,有些失祯)

代码主要就是绘制验证码类的实现

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.drawing;
using system.io;

namespace securitycodepic
{
 public class drawingsecuritycode
 {
  /// <summary>
  /// 生成验证码,并返回
  /// </summary>
  /// <returns></returns>
  public string getsecuritycode(int n)
  {
   string code = generatecheckcode(n);
   createcheckcodeimage(code);
   return code;
  }

  /// <summary>
  /// 动态生成指定数目的随机数或字母
  /// </summary>
  /// <param name="num">整数</param>
  /// <returns>返回验证码字符串</returns>
  private string generatecheckcode(int num)
  {
   int number;//定义变量
   char code;
   string checkcode = string.empty; //空字符串,只读
   random random = new random(); //定义随机变量实例
   for (int i=0; i < num;i++ )
   {
    //利用for循环生成指定数目的随机数或字母
    number = random.next(); //返回一个小于指定的最大值的非负的随机数 next有三个构造函数 
    if (number % 2 == 0)
    {//产生一个一位数
     code = (char)('0' + (char)(number % 10));
    }
    else
    { //产生一个大写字母
     code = (char)('a'+(char)(number % 26));
    }
    checkcode += code.tostring();
   }
   return checkcode;
  }

  /// <summary>
  /// 根据验证码字符串生成验证码图片
  /// </summary>
  /// <param name="checkcode">验证码字符串</param>
  private void createcheckcodeimage(string checkcode)
  {

   if (checkcode == null || checkcode.trim() == string.empty) return;
   // 引用system.drawing类库
   bitmap myimage = new bitmap(80, 30);//生成一个指定大小的位图
   graphics graphics = graphics.fromimage(myimage); //从一个位图生成一个画布
   try
   {
    graphics.clear(color.white); //清除整个绘画面并以指定的背景色填充,这里是把背景色设为白色
    random random = new random(); //实例化一个伪随机数生成器

    //画图片的前景噪音点,这里有100个
    for (int i = 0; i < 100; i++)
    {
     int x = random.next(myimage.width);
     int y = random.next(myimage.height);
     myimage.setpixel(x, y, color.fromargb(random.next()));//指定坐标为x,y处的像素的颜色
    }

    //画图片的背景噪音线,这里为2条
    for (int i = 0; i < 2; i++)
    {
     int x1 = random.next(myimage.width);
     int x2 = random.next(myimage.width);
     int y1 = random.next(myimage.height);
     int y2 = random.next(myimage.height);
     graphics.drawline(new pen(color.black), x1, y1, x2, y2); //绘制一条坐标x1,y1到坐标x2,y2的指定颜色的线条,这里的线条为黑色
    }

    font font = new font("arial", 15, fontstyle.bold); //定义特定的文本格式,这里的字体为arial,大小为15,字体加粗
    //根据矩形、起始颜色和结束颜色以及方向角度产生一个lineargradientbrush实例---线性渐变
    system.drawing.drawing2d.lineargradientbrush brush =
     new system.drawing.drawing2d.lineargradientbrush(
      new rectangle(0, 0, myimage.width, myimage.height),//在坐标0,0处实例化一个和myimage同样大小的矩形
      color.blue, color.red, 1.2f, true);
    //绘制文本字符串
    graphics.drawstring(checkcode, font, brush, 2, 2);

    //绘制有坐标对、宽度和高度指定的矩形---画图片的边框线
    graphics.drawrectangle(new pen(color.silver), 0, 0, myimage.width - 1, myimage.height - 1);
    //创建其支持存储器为内存的流
    memorystream ms = new memorystream();
    //将此图像以指定格式保存到指定的流中
    myimage.save(ms, system.drawing.imaging.imageformat.gif); //这里是以gif的格式保存到内存中
    httpcontext.current.response.clearcontent(); //清除缓冲区流中的所有内容输出
    httpcontext.current.response.contenttype = "image/gif"; //获取或设置输出流的http mime类型
    httpcontext.current.response.binarywrite(ms.toarray()); //将一个二进制字符串写入http输出流
   }
   finally
   {
    //释放占用资源
    graphics.dispose();
    myimage.dispose();
   }
  }
 }
}


然后使用securitycode.ashx文件调用上面类的方法实现

using system;
using system.collections.generic;
using system.linq;
using system.web;

namespace securitycodepic
{
 /// <summary>
 /// securitycode1 的摘要说明
 /// </summary>
 public class securitycode1 : ihttphandler
 {

  public void processrequest(httpcontext context)
  {
   drawingsecuritycode sc = new drawingsecuritycode();
   string securitycode = sc.getsecuritycode(6);
  }

  public bool isreusable
  {
   get
   {
    return false;
   }
  }
 }
}


最后就是asp.net页面图片路径的引用了

<%@ page language="c#" autoeventwireup="true" codebehind="securitycode_test.aspx.cs" inherits="securitycodepic.securitycode_test" %>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>验证码的实现</title>
 <style type="text/css">
 #vcodeimg { cursor: pointer;}
 </style>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <img id="vcodeimg" src="securitycode.ashx" alt="验证码" onclick="javascript:refreshcode();" />
 </div>
 </form>
 <script type="text/javascript">
  function refreshcode() {
   var random = math.random();
   var img = document.getelementbyid("vcodeimg");
   img.src = "securitycode.ashx?" + random; //加上无意义的随机参数,浏览器才会认为是新地址,就会重新读取数据
  }
 </script>
</body>
</html>

以上就是本文的全部,对了,还有源码下载分享给大家,欢迎大家下载。

源码分享:

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

相关文章:

验证码:
移动技术网