Build a backend management system for ASP. Mvc4+ef5+easyui+unity2.x Injection (16)-Privilege management system-Beautiful verification code

Source: Internet
Author: User

We built a database table in the last section, but I found a lot of things have not been perfected, such as verification code, we first do a verification code bar, verification code we are familiar with, in order to prevent malicious login, we must add a verification code on the login page, below I will share a verification code, this is a C # painting, the principle is, Generate a random 4-digit number, save it as a session or a cookie, and compare the verification code entered by the user,

The captcha can be a view cshtml, an ASPX page, or a ashx generic handler, which we use ASHX to display

In the core folder under App.admin, create a new generic processing file verify_code.ashx, copy the following code (this CAPTCHA applies to the ASPX program)

usingSystem;usingSystem.IO;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingsystem.web;usingSystem.Web.SessionState;namespaceapp.admin{/// <summary>    ///Verification code Generation class/// </summary>     Public classVerify_code:ihttphandler, IRequiresSessionState { Public voidProcessRequest (HttpContext context) {intCodew = the; intCodeh = A; intFontSize = -; stringChkcode =string.            Empty; //List of colors for verification codes, noise lines, noise pointscolor[] 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 captcha, removing some easily confusing characters            Char[] character = {'2','3','4','5','6','8','9','a','b','D','e','F','h','k','m','N','R','x','y','A','B','C','D','E','F','G','H','J','K','L','M','N','P','R','S','T','W','X','Y' }; Random Rnd=NewRandom (); //Generate Authenticode String             for(inti =0; I <4; i++) {Chkcode+=Character[rnd. Next (character.            Length)]; }            //Write SessionContext. session["Code"] =Chkcode; //Create a canvasBitmap BMP =NewBitmap (Codew, Codeh); Graphics g=graphics.fromimage (BMP);            G.clear (Color.White); //Draw Noise Line             for(inti =0; I <1; i++)            {                intX1 =Rnd.                Next (Codew); intY1 =Rnd.                Next (Codeh); intx2 =Rnd.                Next (Codew); inty2 =Rnd.                Next (Codeh); Color CLR=Color[rnd. Next (color.                Length)]; G.drawline (NewPen (CLR), x1, y1, x2, y2); }            //draw a Captcha string             for(inti =0; i < chkcode.length; i++)            {                stringFNT =Font[rnd. Next (font.                Length)]; Font ft=NewFont (FNT, fontSize); Color CLR=Color[rnd. Next (color.                Length)]; g.DrawString (Chkcode[i]. ToString (), FT,NewSolidBrush (CLR), (floatI -+2, (float)0); }            //Draw Noise points             for(inti =0; I < -; i++)            {                intx =Rnd. Next (BMP.                Width); inty =Rnd. Next (BMP.                Height); Color CLR=Color[rnd. Next (color.                Length)]; Bmp.            SetPixel (x, Y, CLR); }            //clear the page output cache, set the page without cachingContext. 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 a captcha picture to the memory stream and outputs it in "image/png" formatMemoryStream ms =NewMemoryStream (); Try{BMP.                Save (MS, Imageformat.png); Context.                Response.clearcontent (); Context. Response.ContentType="Image/png"; Context. Response.BinaryWrite (Ms.            ToArray ()); }            finally            {                //explicitly releasing resourcesbmp.                Dispose ();            G.dispose (); }        }         Public BOOLisreusable {Get            {                return false; }        }    }}
View Code

Write session
Context. session["Code" = Chkcode;
Call this page will generate a session, we will compare with the user input code, OK back to the account's index view

Embed this page in the location of the verification code

<tr>                            <td style= "width:80px; Text-align:right" > Verification Code:                            </td>                            <td>                                 <input Style= "width:50px" type= "text" name= "Validatecode" id= "Validatecode"/>                                                                <a href=" javascript:$ (' #codeImg '). Trigger (' click ') ' > not clear? </a>                            </td>                        </tr>

Preview after compiling

Build a backend management system for ASP. Mvc4+ef5+easyui+unity2.x Injection (16)-Privilege management system-Beautiful 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.