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

asp.net mvc验证码类使用

程序员文章站 2022-05-14 21:20:35
验证码类 namespace qjw.verifycode { //用法: //public filecontentresult createv...

验证码类

namespace qjw.verifycode
{
  //用法:
  //public filecontentresult createvalidate()
  //{
  //  validatecode vcode = new validatecode();
  //  string code = vcode.createvalidatecode(5);
  //  session["validatecode"] = code;
  //  byte[] bytes = vcode.createvalidategraphic(code);
  //  return file(bytes, "image/jpeg");
  //}


  public class validatecode
  {
    public validatecode()
    {
    }
    /// <summary>
    /// 验证码的最大长度
    /// </summary>
    public int maxlength
    {
      get { return 10; }
    }
    /// <summary>
    /// 验证码的最小长度
    /// </summary>
    public int minlength
    {
      get { return 1; }
    }
    /// <summary>
    /// 生成验证码
    /// </summary>
    /// <param name="length">指定验证码的长度</param>
    /// <returns></returns>
    public string createvalidatecode(int length)
    {
      int[] randmembers = new int[length];
      int[] validatenums = new int[length];
      string validatenumberstr = "";
      //生成起始序列值
      int seekseek = unchecked((int)datetime.now.ticks);
      random seekrand = new random(seekseek);
      int beginseek = (int)seekrand.next(0, int32.maxvalue - length * 10000);
      int[] seeks = new int[length];
      for (int i = 0; i < length; i++)
      {
        beginseek += 10000;
        seeks[i] = beginseek;
      }
      //生成随机数字
      for (int i = 0; i < length; i++)
      {
        random rand = new random(seeks[i]);
        int pownum = 1 * (int)math.pow(10, length);
        randmembers[i] = rand.next(pownum, int32.maxvalue);
      }
      //抽取随机数字
      for (int i = 0; i < length; i++)
      {
        string numstr = randmembers[i].tostring();
        int numlength = numstr.length;
        random rand = new random();
        int numposition = rand.next(0, numlength - 1);
        validatenums[i] = int32.parse(numstr.substring(numposition, 1));
      }
      //生成验证码
      for (int i = 0; i < length; i++)
      {
        validatenumberstr += validatenums[i].tostring();
      }
      return validatenumberstr;
    }
    /// <summary>
    /// 创建验证码的图片
    /// </summary>
    /// <param name="containspage">要输出到的page对象</param>
    /// <param name="validatenum">验证码</param>
    public byte[] createvalidategraphic(string validatecode)
    {
      bitmap image = new bitmap((int)math.ceiling(validatecode.length * 12.0), 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 font("arial", 12, (fontstyle.bold | fontstyle.italic));
        lineargradientbrush brush = new lineargradientbrush(new rectangle(0, 0, image.width, image.height),
         color.blue, color.darkred, 1.2f, true);
        g.drawstring(validatecode, font, brush, 3, 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);
        //保存图片数据
        memorystream stream = new memorystream();
        image.save(stream, imageformat.jpeg);
        //输出图片流
        return stream.toarray();
      }
      finally
      {
        g.dispose();
        image.dispose();
      }
    }
    /// <summary>
    /// 得到验证码图片的长度
    /// </summary>
    /// <param name="validatenumlength">验证码的长度</param>
    /// <returns></returns>
    public static int getimagewidth(int validatenumlength)
    {
      return (int)(validatenumlength * 12.0);
    }
    /// <summary>
    /// 得到验证码的高度
    /// </summary>
    /// <returns></returns>
    public static double getimageheight()
    {
      return 22.5;
    }
  }
}

使用方法

    public filecontentresult createvalidate()
    {
      validatecode vcode = new validatecode();
      string code = vcode.createvalidatecode(5);
      session["validatecode"] = code;
      byte[] bytes = vcode.createvalidategraphic(code);

      return file(bytes, "image/jpeg");
    }

html
<a href="javascript:;" onclick="reloadcode();"><img id="safecode" src="/common/createvalidate" alt="" /></a> 
视图js 

function reloadcode() {
    var verify = document.getelementbyid('safecode');
    verify.setattribute('src', '/common/createvalidate?' + math.random());
  }

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