ASP.NET中怎么实现验证码
创始人
2024-11-28 17:23:01
0次

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;

namespace SecurityCodePic
{
 public class DrawingSecurityCode
 {
  /// 
  /// 生成验证码,并返回
  /// 
  /// 
  public string GetSecurityCode(int n)
  {
   string code = GenerateCheckCode(n);
   CreateCheckCodeImage(code);
   return code;
  }

  /// 
  /// 动态生成指定数目的随机数或字母
  /// 
  /// 整数
  /// 返回验证码字符串
  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;
  }

  /// 
  /// 根据验证码字符串生成验证码图片
  /// 
  /// 验证码字符串
  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
{
 /// 
 /// SecurityCode1 的摘要说明
 /// 
 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" %>





 验证码的实现
 
 #VCodeImg { cursor: pointer;}
 


 
 
   
      function RefreshCode() {    var random = Math.random();    var img = document.getElementById("VCodeImg");    img.src = "SecurityCode.ashx?" + random; //加上无意义的随机参数,浏览器才会认为是新地址,就会重新读取数据   }  

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...