RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
怎么使用ASP.NET实现生成验证码功能-创新互联

这篇文章给大家分享的是有关怎么使用ASP.NET实现生成验证码功能的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

站在用户的角度思考问题,与客户深入沟通,找到大冶网站设计与大冶网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、网站制作、企业官网、英文网站、手机端网站、网站推广、域名注册网站空间、企业邮箱。业务覆盖大冶地区。

生成验证码原理:产生随机字符,并将字符生成为图片,同时储存到Session里去,然后验证用户输入的内容是否与Session中的验证码相符即可。

一般处理程序:CheckCodeHandler.cs

<%@ WebHandler Language="C#" Class="CheckCodeHandler" %>
using System;
using System.Web;
using System.Text;
using System.Drawing;
using System.Web.SessionState;
public class CheckCodeHandler : IHttpHandler,IRequiresSessionState
{
  //产生验证码的字符集
  public string charcode = "2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,R,S,U,W,X,Y,a,b,c,d,e,f,g,h,j,k,m,n,p,r,s,u,w,x,y";
  public void ProcessRequest (HttpContext context) {
    string validateCode = CreateRandomCode(4);
    context.Session["ValidateCode"] = validateCode;//将验证码保存到session中
    CreateCodeImage(validateCode, context);
  }
  public bool IsReusable {
    get {
      return false;
    }
  }
  /// 
  /// 生成验证码
  /// 
  /// 验证码个数
  /// 验证码字符串
  public string CreateRandomCode(int n)
  {
    string[] CharArray = charcode.Split(',');//将字符串转换为字符数组
    string randomCode = "";
    int temp = -1;
    Random rand = new Random();
    for (int i = 0; i < n; i++)
    {
      if (temp != -1)
      {
        rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
      }
      int t = rand.Next(CharArray.Length - 1);
      if (temp != -1 && temp == t)
      {
        return CreateRandomCode(n);
      }
      temp = t;
      randomCode += CharArray[t];
    }
    return randomCode;
  }
  public void CreateCodeImage(string checkCode, HttpContext context)
  {
    int iwidth = (int)(checkCode.Length * 13);
    System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
    Graphics g = Graphics.FromImage(image);
    Font f = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold));
    // 前景色
    Brush b = new System.Drawing.SolidBrush(Color.Black);
    // 背景色
    g.Clear(Color.White);
    // 填充文字
    g.DrawString(checkCode, f, b, 0, 1);
    // 随机线条
    Pen linePen = new Pen(Color.Gray, 0);
    Random rand = new Random();
    for (int i = 0; i < 5; i++)
    {
      int x1 = rand.Next(image.Width);
      int y1 = rand.Next(image.Height);
      int x2 = rand.Next(image.Width);
      int y2 = rand.Next(image.Height);
      g.DrawLine(linePen, x1, y1, x2, y2);
    }
    // 随机点
    for (int i = 0; i < 30; i++)
    {
      int x = rand.Next(image.Width);
      int y = rand.Next(image.Height);
      image.SetPixel(x, y, Color.Gray);
    }
    // 边框
    g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);
    // 输出图片
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    context.Response.ClearContent();
    context.Response.ContentType = "image/jpeg";
    context.Response.BinaryWrite(ms.ToArray());
    g.Dispose();
    image.Dispose();
  }
}

封装成类库:ValidateNumber.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
/// 
///ValidateNumber 生成验证码
/// 
public class ValidateNumber
{
  //产生验证码的字符集 (易混淆的字符去掉)
  private string charcode = "2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,R,S,U,W,X,Y,a,b,c,d,e,f,g,h,j,k,m,n,p,r,s,u,w,x,y";
  /// 
  /// 验证码的大长度
  /// 
  public int MaxLength
  {
    get { return 10; }
  }
  /// 
  /// 验证码的最小长度
  /// 
  public int MinLength
  {
    get { return 1; }
  }
  /// 
  /// 生成验证码
  /// 
  /// 指定验证码的长度
  /// 
  public string CreateValidateNumber(int length)
  {
    string[] CharArray = charcode.Split(',');//将字符串转换为字符数组
    string randomCode = "";
    int temp = -1;
    Random rand = new Random();
    for (int i = 0; i < length; i++)
    {
      if (temp != -1)
      {
        rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
      }
      int t = rand.Next(CharArray.Length - 1);
      if (temp != -1 && temp == t)
      {
        return CreateValidateNumber(length);
      }
      temp = t;
      randomCode += CharArray[t];
    }
    return randomCode;
  }
  /// 
  /// 创建验证码的图片
  /// 
  /// context对象
  /// 验证码
  public void CreateValidateGraphic(HttpContext context,string validateNum)
  {
    int iwidth = (int)(validateNum.Length * 14);
    Bitmap image = new Bitmap(iwidth, 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(validateNum, 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);
      //输出图片
      context.Response.Clear();
      context.Response.ContentType = "image/jpeg";
      context.Response.BinaryWrite(stream.ToArray());
    }
    finally
    {
      g.Dispose();
      image.Dispose();
    }
  }
  /// 
  /// 得到验证码图片的长度
  /// 
  /// 验证码的长度
  /// 
  public static int GetImageWidth(int validateNumLength)
  {
    return (int)(validateNumLength * 14);
  }
  /// 
  /// 得到验证码图片的高度
  /// 
  /// 
  public static double GetImageHeight()
  {
    return 22;
  }
}

感谢各位的阅读!关于怎么使用ASP.NET实现生成验证码功能就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:怎么使用ASP.NET实现生成验证码功能-创新互联
本文URL:http://scyingshan.cn/article/gjpcj.html