MVC generates security Verification code (example: for login verification) (Code turn)

Source: Internet
Author: User

---------HTML

<td> captcha:</td>           <td>                              <div style= "float:right; margin-top:5px;" >                   <a href= "javascript:void (0)" onclick= "Clickremovechangecode (); return false;" > can not see Clearly, change a </a>               </div>           </td>

-----------jquery

Click Re-change the CAPTCHA function Clickremovechangecode () {    var code = $ ("#imgCode"). attr ("src");    $ ("#imgCode"). attr ("src", Code + "1");}   

------------ASP. NET MVC returns file address

<summary>///Verification Code///</summary>///<param name= "context" ></param>///<returns> </returns>public ActionResult Checkcode () {    //Generate Verification code    Validatecode Validatecode = new Validatecode ();    String code = Validatecode.createvalidatecode (4);    session["Validatecode"] = code;    byte[] bytes = validatecode.createvalidategraphic (code);    Return File (bytes, @ "Image/jpeg");}

Class----generate verification codes and draw backgrounds

public class Validatecode {public Validatecode () {}///<summary>///verification code max.        Length///</summary> public int MaxLength {get {return 10;}            }///<summary>///Minimum length of verification code///</summary> public int MinLength {        get {return 1;} }///<summary>//Generate verification codes///</summary>//<param name= "Length" > Specify the lengths of the Captcha &L            t;/param>//<returns></returns> public string createvalidatecode (int length) {            int[] randmembers = new Int[length];            int[] validatenums = new Int[length];            String validatenumberstr = "";            Generates the starting sequence value int seekseek = unchecked ((int) DateTime.Now.Ticks);            Random Seekrand = new Random (Seekseek);            int beginseek = (int) seekrand.next (0, Int32.maxvalue-length * 10000); Int[] seeks = new Int[length];                for (int i = 0; i < length; i++) {Beginseek + = 10000;            Seeks[i] = Beginseek; }//Generate random numbers for (int i = 0; i < length; i++) {Random rand = new Random (                Seeks[i]);                int pownum = 1 * (int) Math.pow (ten, length); Randmembers[i] = rand.            Next (Pownum, Int32.MaxValue); }//extract random numbers for (int i = 0; i < length; i++) {String numstr = Randmembe Rs[i].                ToString ();                int numlength = Numstr.length;                Random rand = new Random (); int numposition = rand.                Next (0, numLength-1);            Validatenums[i] = Int32.Parse (numstr.substring (numposition, 1)); }//Generate Verification code for (int i = 0; i < length; i++) {validatenumberstr + = valid Atenums[i].            ToString (); } return ValidatenumbersTr }//c# MVC Upgrade///<summary>//Create a picture of the CAPTCHA////</summary>//<param Nam E= "Containspage" > Page object to Output to </param>//<param name= "Validatenum" > Verification Code </param> public B Yte[] Createvalidategraphic (string validatecode) {Bitmap image = new Bitmap ((int) math.ceiling (Validatec Ode.            Length * 12.0), 22);            Graphics g = graphics.fromimage (image);                try {//Generate random generator randomly random = new random ();                Empty the picture background color g.clear (color.white); The interference line for the picture is (int i = 0; i <; 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", 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 (Validatecode, Font, Brush, 3, 2); The foreground of the painting picture interferes with the 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 ())); }//Draw the border line of the picture G.drawrectangle (new Pen (Color.silver), 0, 0, image. Width-1, image.                HEIGHT-1);                Save picture Data MemoryStream stream = new MemoryStream (); Image.                Save (stream, imageformat.jpeg); Output picture stream return stream.            ToArray (); } finally {G.dispoSE (); Image.            Dispose (); }}///<summary> Get the length of the CAPTCHA picture///</summary>//<param name= "Valida  Tenumlength "> Length of verification code </param>//<returns></returns> public static int getimagewidth (int        Validatenumlength) {return (int) (VALIDATENUMLENGTH * 12.0);        }//<summary>//The height of the verification code///</summary>//<returns></returns>        public static double Getimageheight () {return 22.5; }     }

  

MVC generates security Verification code (example: for login verification) (Code turn)

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.