A simple ASP. NET verification code

  • 2021-09-24 21:58:02
  • OfStack

In this paper, we share the specific code of ASP. NET verification code for your reference. The specific contents are as follows

I mainly see the interference line. If there is no interference line in a verification code, at least I have to work hard on the layout of noise and random code:


 /// <summary>
 ///  Verification code generation class 
 /// </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 list for verification code, noise line and noise point  
   Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
   // Font list for verification code  
   string[] font = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
   // The character set of the verification code has been removed 1 Some confusing characters  
   char[] character = { '0', '1', '2', '3', '4', '5', '6', '8', '9' };
   Random rnd = new Random();
   // Generate a verification code string  
   for (int i = 0; i < 4; i++)
   {
    chkCode += character[rnd.Next(character.Length)];
   }
   // Write Session
   context.Session["sys_verify_code"] = chkCode;
   // Create a canvas 
   Bitmap bmp = new Bitmap(codeW, codeH);
   Graphics g = Graphics.FromImage(bmp);
   g.Clear(Color.White);
   // Draw noisy lines  
   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);
   }
   // Draw verification code string  
   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);
   }
   // Drawing noise  
   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);
   }
   // Clear the page output cache and set the page to have no cache  
   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");
   // Writes the verification code picture to the memory stream and puts it in the  "image/Png"  Format output  
   MemoryStream ms = new MemoryStream();
   try
   {
    bmp.Save(ms, ImageFormat.Png);
    context.Response.ClearContent();
    context.Response.ContentType = "image/Png";
    context.Response.BinaryWrite(ms.ToArray());
   }
   finally
   {
    // Explicitly releasing resources  
    bmp.Dispose();
    g.Dispose();
   }
  }

  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
 }

Basic validation generation code 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);
  // Background color 
  g.Clear(Color.White);
  // Text font 
  Font f = new Font("Arial Black", 10);
  // Text color 
  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;
 }

}


Related articles: