首页后端开发ASP.NETASP.NET验证码制作

ASP.NET验证码制作

时间2024-01-30 15:52:02发布访客分类ASP.NET浏览919
导读:收集整理的这篇文章主要介绍了ASP.NET验证码制作,觉得挺不错的,现在分享给大家,也给大家做个参考。这篇文章主要为大家详细介绍了一个简单的ASP.NET验证码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下本文实例为大家分享了ASP.N...
收集整理的这篇文章主要介绍了ASP.NET验证码制作,觉得挺不错的,现在分享给大家,也给大家做个参考。这篇文章主要为大家详细介绍了一个简单的ASP.NET验证码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了ASP.NET验证码的具体代码,供大家参考,具体内容如下

我主要是看到干扰线了,一个验证码里面要是没有干扰线什么的,至少得在噪点和随机码的排版上下工夫:

@H_406_5@
 /// summary>
     /// 验证码生成类 /// /summary>
 public class verify_code : IHttpHandler, IRequiresSessionstate {
  public void PRocessRequest(HttpContext context)  {
       int codeW = 80;
       int codeH = 22;
       int fontSize = 16;
       string chkCode = string.Empty;
   //颜色列表,用于验证码、噪线、噪点    Color[] color = {
 Color.Black, Color.red, Color.Blue, Color.Green, Color.Orange, Color.brown, Color.Brown, Color.DarkBlue }
    ;
   //字体列表,用于验证码    string[] font = {
 "Times New roman", "Verdana", "Arial", "Gungsuh", "Impact" }
    ;
   //验证码的字符集,去掉了一些容易混淆的字符    char[] character = {
 '0', '1', '2', '3', '4', '5', '6', '8', '9' }
    ;
       Random rnd = new Random();
       //生成验证码字符串    for (int i = 0;
     i  4;
 i++)   {
        chkCode += character[rnd.Next(character.Length)];
   }
       //写入Session   context.Session["Sys_verify_code"] = chkCode;
       //创建画布   BITmap bmp = new Bitmap(codeW, codeH);
       Graphics g = Graphics.FromImage(bmp);
       g.Clear(Color.White);
       //画噪线    for (int i = 0;
     i  4;
 i++)   {
        int x1 = rnd.Next(codeW);
        int y1 = rnd.Next(codeH);
        int x2 = rnd.Next(codeW);
        int y2 = rnd.Next(codeH);
        Color clr = color[rnd.Next(color.Length)];
        g.DrawLine(new PEn(clr), x1, y1, x2, y2);
   }
       //画验证码字符串    for (int i = 0;
     i  chkCode.Length;
 i++)   {
        string fnt = font[rnd.Next(font.Length)];
        Font ft = new Font(fnt, fontSize);
        Color clr = color[rnd.Next(color.Length)];
        g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18 + 2, (float)0);
   }
       //画噪点    for (int i = 0;
     i  100;
 i++)   {
        int x = rnd.Next(bmp.Width);
        int y = rnd.Next(bmp.Height);
        Color clr = color[rnd.Next(color.Length)];
        bmp.SetPixel(x, y, clr);
   }
       //清除该页输出缓存,设置该页无缓存    context.Response.Buffer = true;
       context.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
       context.Response.Expires = 0;
       context.Response.CacheControl = "no-cache";
       context.Response.AppendHeader("Pragma", "No-Cache");
       //将验证码图片写入内存流,并将其以 "image/Png" 格式输出    MemoryStream ms = new MemoryStream();
   try   {
        bmp.Save(ms, ImageFormat.Png);
        context.Response.ClearContent();
        context.Response.ContentType = "image/Png";
        context.Response.BinaryWrite(ms.ToArray());
   }
   finally   {
        //显式释放资源     bmp.Dispose();
        g.Dispose();
   }
  }
  public bool IsReusable  {
   get   {
        return false;
   }
  }
 }
    

基本验证生成代码demo:


using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;
public partial class image : System.Web.UI.Page{
 protected void Page_Load(object sender, Eventargs e) {
      string tmp = RndNum(4);
      HttpCookie a = new HttpCookie("ImageV", tmp);
      Response.Cookies.Add(a);
      this.ValidateCode(tmp);
 }
 private void ValidateCode(string VNum) {
      Bitmap img = null;
      Graphics g = null;
      MemoryStream ms = null;
      int gheight = VNum.Length * 12;
      Img = new Bitmap(gheight, 25);
      g = Graphics.FromImage(Img);
      //背景颜色  g.Clear(Color.White);
      //文字字体  Font f = new Font("Arial Black", 10);
      //文字颜色  SolidBrush s = new SolidBrush(Color.Black);
      g.DrawString(VNum, f, s, 3, 3);
      ms = new MemoryStream();
      Img.Save(ms, ImageFormat.Jpeg);
      Response.ClearContent();
      Response.ContentType = "image/Jpeg";
      Response.BinaryWrite(ms.ToArray());
      g.Dispose();
      Img.Dispose();
      Response.End();
 }
 private string RndNum(int VcodeNum) {
      string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +   ",q,r,s,t,u,v,w,x,y,z";
  string[] VcArray = Vchar.Split(new Char[] {
 ',' }
    );
      string VNum = "";
      int temp = -1;
      Random rand = new Random();
      for (int i = 1;
     i  VcodeNum + 1;
 i++)  {
   if (temp != -1)   {
        rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
   }
       int t = rand.Next(35);
       if (temp != -1 &
    &
 temp == t)   {
        return RndNum(VcodeNum);
   }
       temp = t;
       VNum += VcArray[t];
  }
      return VNum;
 }
}
    

以上就是ASP.NET验证码制作的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

ASP.NET制作

若转载请注明出处: ASP.NET验证码制作
本文地址: https://pptw.com/jishu/592724.html
javascript数组sort方法怎么用 MVC5限制所有HTTP必须以POST方式请求

游客 回复需填写必要信息