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

ASP.NET验证码(3种)

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

阿路龟,3366小游戏大全3366,麻仓优torrent

日常生活中我们在使用网站时都会遇到验证码,大家有没有想过为什么要使用验证码?

其实验证码的作用就是防止恶意破解密码、刷票、论坛灌水、刷页。有效防止某个黑客对某一个特定注册用户用特定程序暴力破解方式进行不断的登录尝试。今天就跟大家分享asp.net的三种验证码。

1.gsc_webcontrollibrary 这是在网上找到的一个控件,非常好用。但是效果不是特别好(见下图。
)虽然容易使用,所有的属性都可以像控件一样设置,但是可用性不太高。用户不能自定义,而且看起来这个验证码效果不太好。

效果:

2.用一个页面生成图片,另一个页面调用,验证码存入cookie,调用时取cookie对比验证.这个用户就可以按自己的喜好更改效果和验证码的长度了。

效果如图:

checkcode.aspx代码如下:

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;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;

public partial class tools_checkcode : system.web.ui.page
{
  protected void page_load(object sender, eventargs e)
  {
    this.createcheckcodeimage(generatecheckcode());

  }

  private string generatecheckcode()
  {
    int number;
    char code;
    string checkcode = string.empty;

    system.random random = new random();

    for (int i = 0; i < 5; 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 < 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.greenyellow), x1, y1, x2, y2);
      }

      font font = new system.drawing.font("verdana", 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 < 80; 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.red), 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();
    }
  }


}

然后在需要使用的页面引用:
usecheckcode.aspx

<img src="tools/checkcode.aspx" alt="验证码" style="width: 60px; height: 24px" />

3.用web handler生成图片。这个其实和前面的意思大致差不多,调用方法也基本和2一样,不同的是,它的验证码是存入session的。供学习参考。

效果图如下:

validateimagehandler.ashx

%@ webhandler language="c#" class="validateimagehandler" %>

using system;
using system.web;
using system.web.sessionstate;
using system.drawing;
using system.drawing.imaging;
using system.text;

/// <summary>
/// validateimagehandler 生成网站验证码功能
/// </summary>
public class validateimagehandler : ihttphandler, irequiressessionstate
{
  int intlength = 5;        //长度
  string stridentify = "identify"; //随机字串存储键值,以便存储到session中
  public validateimagehandler()
  {    
  }

  /// <summary>
  /// 生成验证图片核心代码
  /// </summary>
  /// <param name="hc"></param>
  public void processrequest(httpcontext hc)
  {
    //设置输出流图片格式
    hc.response.contenttype = "image/gif";
    
    bitmap b = new bitmap(200, 60);
    graphics g = graphics.fromimage(b);
    g.fillrectangle(new solidbrush(color.yellowgreen), 0, 0, 200, 60);
    font font = new font(fontfamily.genericserif, 48, fontstyle.bold, graphicsunit.pixel);
    random r = new random();

    //合法随机显示字符列表
    string strletters = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890";
    stringbuilder s = new stringbuilder();
    
    //将随机生成的字符串绘制到图片上
    for (int i = 0; i < intlength; i++)
    {
      s.append(strletters.substring(r.next(0, strletters.length - 1), 1));
      g.drawstring(s[s.length - 1].tostring(), font, new solidbrush(color.blue), i * 38, r.next(0, 15));
    }

    //生成干扰线条
    pen pen = new pen(new solidbrush(color.blue), 2);
    for (int i = 0; i < 10; i++)
    {
      g.drawline(pen, new point(r.next(0, 199), r.next(0, 59)), new point(r.next(0, 199), r.next(0, 59)));
    }
    b.save(hc.response.outputstream, imageformat.gif);
    hc.session[stridentify] = s.tostring(); //先保存在session中,验证与用户输入是否一致
    hc.response.end();
  
  }
  
  /// <summary>
  /// 表示此类实例是否可以被多个请求共用(重用可以提高性能)
  /// </summary>
  public bool isreusable
  {
    get
    {
      return true;
    }
  }
}

以上就是asp.net3种验证码,各有各的优缺点,希望大家可以熟练实现不同验证码的功能。

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

相关文章:

验证码:
移动技术网