asp.net Login Verification Code Implementation Method _ Practical skills

Source: Internet
Author: User

tags and methods added to the front end:
Verification Code:

Copy Code code as follows:
<input id= "Txtverifycode" type= "text" maxlength= "5" style= "line-height:30px; height:30px; Width:80px;border:solid 1px #d4d4d4; "class=" input "/> Click on the picture Refresh </p>//label

$ (function () {
      $ ("#imgValidateCode"). Click (function () {
        dofresh ();
      });
      Dofresh ();
    })
function Dofresh () {
      var img = $ ("#imgValidateCode");
      Img.attr ("src", "verifycode.aspx?random=" + math.random ());

    } Added method, SRC is the address of the ASPX that generated the picture 

Then in the project, in a new verifycode.aspx, the following is the code for ASPX:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "VerifyCode.aspx.cs" inherits= "Form.verifycode"%> 
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

Next is the Aspx.cs code:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;

Using System.Web.UI.WebControls;

 

Using System.Drawing;
    namespace Form {public partial class VerifyCode:System.Web.UI.Page {public static string HZ;

    <summary> the maximum length of the///verification code///</summary> public int MaxLength {get {return 10;}  The minimum length of the///<summary>///Verification Code///</summary> public int MinLength {get {1;
      } protected void Page_Load (object sender, EventArgs e) {string[] str = createvalidatenumber (4); String strcode = String.
      Empty; for (int i = 0; i < str. Length;

      i++) {Strcode + = Str[i];
      } createcheckcodeimage (str);
      HZ = Strcode;
     Response.Write (HZ);
    The verification code is stored in the session session["Validatecode"] = HZ; ///<summary>///generate Authenticode///</summary>///<param name= "Length" &Gt; Specify the length of the authentication code </param>///<returns> Authenticode </returns> public string[] Createvalidatenumber (int length) {String Vchar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p" + ", Q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f,g,

      H,i,j,k,l,m,n,p,q "+", r,s,t,u,v,w,x,y,z ";

      string[] Vcarray = Vchar.split (new char[] {', '});//split into array string[] num = new String[length];

      int temp = -1;//records the last random number, avoiding the production of a few of the same random numbers Random rand = new Random ();

          A simple algorithm is used to ensure that the random number is generated for a different for (int i = 1; i < length + 1; i++) {if (temp!=-1) {

        Rand = new Random (i * temp * unchecked ((int) DateTime.Now.Ticks)); int t = rand.
        Next (vcarray.length-1);

 

        if (temp!=-1 && temp = = t) {return createvalidatenumber (length);
        temp = t;
        NUM[I-1] = vcarray[t]; Num.
        SetValue (Vcarray[t]);

      Vnum + = vcarray[t];
 return num;   } private void Createcheckcodeimage (string[] checkcode) {if (Checkcode = = NULL | | checkcode.length <=
      0) return;
      System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) math.ceiling (checkcode.length * 32.5)), 60;
     System.Drawing.Graphics g = graphics.fromimage (image);

       try {//Generate a random generator Random Random = new Random ();

       Clear the picture background color g.clear (color.white); Define color color[] c = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcy

        AN, color.purple}; The background noise line for the picture is for (int i = 0; i < i++) {int cindex = random.
          Next (7); int findex = random.
          Next (5); 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 (C[cindex)), x1, y1, x2, y2);
        }Define font string[] f = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"}; for (int k = 0; k <= checkcode.length-1 k++) {int cindex = random.
          Next (7); int findex = random.
          Next (5);
          Font drawfont = new Font (F[findex], num, (System.Drawing.FontStyle.Bold));
          SolidBrush Drawbrush = new SolidBrush (C[cindex]);
          float x = 5.0F;
          Float y = 0.0F;
          float width = 42.0F;
          float height = 48.0F; int SJX = random.
          Next (10); int sjy = random. Next (image.

          Height-(int) height);
          RectangleF drawrect = new RectangleF (x + SJX + (k), Y + sjy, width, height);
          StringFormat Drawformat = new StringFormat ();
        Drawformat.alignment = Stringalignment.center;
        g.DrawString (Checkcode[k], Drawfont, Drawbrush, DrawRect, Drawformat); ///Picture foreground noise point for (int i = 0; i < i++) {int x = random. Next (iMage.
          Width); int y = random. Next (image. 

          Height); Image. SetPixel (x, Y, Color.FromArgb) (random.

        Next ())); int cindex1 = random.

        Next (7); Draw a picture of the border line G.drawrectangle (new Pen (C[CINDEX1)), 0, 0, image. Width-1, image.
       HEIGHT-1);
        System.IO.MemoryStream ms = new System.IO.MemoryStream (); Image.
        Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
        Response.clearcontent ();
        Response.ContentType = "Image/gif"; Response.BinaryWrite (Ms.

      ToArray ());

        finally {g.dispose (); Image.

      Dispose (); 
 }

    }

  }

}

So! You can generate the verification code, and then you write the verification version of the logic of the judgement can be written!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.