Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (2)--Verification code

Source: Internet
Author: User
Tags mysql code

Write in front

Intermittent, today is the verification code of the east to get out.

Series Articles

[Ef]vs15+ef6+mysql Code First mode

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (1)

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (2)--User Registration

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (2)--Verification code

Verification Code Class
usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacewolfy.netdisk.utilities{/// <summary>    ///Verification Code Class/// </summary>     Public Static classVerifycode {/// <summary>        ///Generate verification Code/// </summary>        /// <param name= "Len" >Verification Code Length</param>        /// <param name= "Strcode" >Verification Code</param>        /// <returns>Verification Code Picture</returns>         Public Static byte[] Create (intLen out stringstrcode) {MemoryStream stream=NewMemoryStream (); byte[] buffer =NULL; //Noise Line Noisecolor[] Colors ={color.black, color.red, Color.Blue, Color.green, Color.orange, Color.brown, color.darkblue}; //Verification Code Font            string[] fonts = {"Times New Roman","MS Mincho","Book Antiqua","Gungsuh","PMingLiU","Impact" }; //Verification Code Content            Char[] Charactars = {'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','S','Y','Z' }; //Generate Authenticode StringStringBuilder SB =NewStringBuilder (); Random R=NewRandom ();  for(inti =0; i < Len; i++)            {                CharCodechar = Charactars[r.next (0, Charactars.                Length)]; Sb.            Append (Codechar); } strcode=sb.            ToString (); using(Bitmap Bitmap =NewBitmap (len * -, *))            {                using(Graphics g =graphics.fromimage (bitmap))                    {g.clear (color.white);  for(inti =0; I <Ten; i++)                    {                        intX1 = R.next ( -); intY1 = R.next ( +); intx2 = R.next ( -); inty2 = R.next ( +); Color Color=colors[r.next (colors.                        Length)]; G.drawline (NewPen (color), x1, y1, x2, y2); }                    //draw a string into a picture                     for(inti =0; i < strcode.length; i++)                    {                        stringFont =Fonts[r.next (fonts.                        Length)]; Font fnt=NewFont (font, -); Color Color=colors[r.next (colors.                        Length)]; g.DrawString (Strcode[i]. ToString (), FNT,NewSolidBrush (color), (floatI -+8, (float)8); }                    //Draw Noise points                     for(inti =0; I < -; i++)                    {                        intx =R.next (bitmap.                        Width); inty =R.next (bitmap.                        Height); Color Color=colors[r.next (colors.                        Length)]; Bitmap.                    SetPixel (x, y, color); }} bitmap.                Save (stream, System.Drawing.Imaging.ImageFormat.Jpeg); Buffer=Stream.            ToArray (); }            returnbuffer; }    }}

Add an action in Userinfocontroller

  [HttpGet]        public  actionresult verifycodeimage ()        {            string string . Empty;             byte [] buffer = verifycode.create (6 out strcode);            session["code"= strcode;             return @" Image/jpeg " );        }

Form input box with verification code added on the registration page

    <Divclass= "Form-group">            <labelclass= "Control-label col-md-2">Verification Code</label>            <Divclass= "Col-md-10">                <inputtype= "text"name= "Name"value=" " />  <imgID= "Imgcode"style= "Cursor:pointer;"title= "Toggle the next One"src= "/userinfo/verifycodeimage"alt= "Verification Code" />            </Div>        </Div>

Add a click event for the CAPTCHA picture, switch to the next one, and to avoid caching, add a random number parameter to ensure that each request is a new request.

<script>    $ (function  () {        $ ("#imgCode"). Click (function  () {             var ran = math.random ();             // plus a random number of ran, avoid the verification code cache. this            . src = "/userinfo/verifycodeimage?_r=" + ran;        })    ;    </script>

Page testing

Summarize

Today than usual to come back a little earlier, while doing a function, the progress is a bit slow ah.

Actual combat MVC5+EF6+MYSQL Enterprise Network Disk Combat (2)--Verification code

Related Article

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.