ASP. NET Write Verification code

Source: Internet
Author: User

Generate service-side code for verification Code and matching verification code

<%@ WebHandler language="C #"class="Validataecodehandler"%>usingSystem;usingsystem.web;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.IO;usingSystem.Drawing.Imaging; Public classValidataecodehandler:ihttphandler, system.web.sessionstate.irequiressessionstate{ Public voidProcessRequest (HttpContext context) {//Request type: Get verification code picture, match verification code        stringType = context. request["type"]; if(Type = ="Math")        {            if(string. IsNullOrEmpty (context. request["Code"]) context. Response.Write (2); Else if(string. IsNullOrEmpty (""+ context. session["Yqcode"+ context. request["ID"]]) {context. Response.Write (3); }            Else            {                if(""+ context. session["Yqcode"+ context. request["ID"]] = = Context. request["Code"] +"") context. Response.Write (1); Elsecontext. Response.Write (0); }        }        Else{context. Response.ContentType="Image/gif"; stringValidatecode = Createvalidatecode (context);//Generate verification CodeBitmap Bitmap =NewBitmap (ImgWidth, imgheight);//Generating bitmap ImagesDisturbbitmap (bitmap);//Image BackgroundDrewvalidatecode (bitmap, Validatecode);//drawing the captcha imageBitmap. Save (context. Response.outputstream, imageformat.gif);//save image, wait for outputcontext.        Response.Write (bitmap); }    }    //private int codelen = 4;//Verification Code Length    Private intFineness = -;//Picture Clarity    Private intImgWidth = -;//Picture Width    Private intImgHeight = -;//Picture Height    Private stringfontFamily ="Times New Roman";//Font name    Private intFontSize = -;//Font Size//private int fontstyle = 0;//font Style    Private intPosX =0;//Draw start coordinates x    Private intPosY =0;//Draw coordinates y    Private stringCreatevalidatecode (HttpContext context)//Generate verification Code    {        stringValidatecode =""; Random Random=NewRandom ();//Random Number ObjectValidatecode = random. Next ( +,9999) +""; //for (int i = 0; i < Codelen; i++)//loop generates each digit value//{        //int n = random. Next (ten);//Digital//Validatecode + = n.tostring (); //}Context. session["Yqcode"+ context. request["ID"]] = Validatecode;//Save Verification Code This session is called at the foreground.         returnValidatecode;//Return Verification Code    }    Private voidDisturbbitmap (Bitmap Bitmap)//Image Background{random random=NewRandom ();//generate by random number         for(inti =0; I < bitmap. Width; i++)//generation by pixel-by-loop nesting        {             for(intj =0; J < Bitmap. Height; J + +)            {                if(Random. Next ( -) <= This. fineness) bitmap.            SetPixel (i, J, Color.lightgray); }        }    }    Private voidDrewvalidatecode (Bitmap Bitmap,stringValidatecode)//drawing the captcha image{Graphics g= Graphics.fromimage (bitmap);//get the Paint objectFont font =NewFont (fontFamily, FontSize, fontstyle.bold);//Set Drawing Fontg.DrawString (Validatecode, Font, Brushes.black, PosX, PosY);//drawing the captcha image    }     Public BOOLisreusable {Get        {            return false; }    }}
View Code

Manually refresh the verification code

<script type= "Text/javascript" >         // Click to refresh captcha         function  f_refreshtype () {              var Image1 = document.getElementById ("Valicode");              if NULL {                 = image1.src + "?") ;             }         }      </script>

Ajax Sync Verification Verification code is correct before submitting form

varUrlcode = '/handler/validataecodehandler.ashx?id=12&type=math&code= ' + $.trim ($ ("#yzcode"). Val ()); //Ajax synchronization Requests        varMathresult = $.ajax ({type: "GET", Url:urlcode, Async:false}). responsetext; if(Mathresult! = 1) {            varImage1 = document.getElementById ("Valicode"); if(Image1! =NULL) {image1.src= Image1.src + "?"; } alert ("Verification code does not match!" "); return false; }  

<Divclass= "line">  <span>Verification Code:</span><inputvalue=""type= "text"name= "Yzcode"ID= "Yzcode"class= "Tong" />  <em><imgID= "Valicode"name= "Valicode"onclick= "F_refreshtype ();"style= "HEIGHT:32PX;"src= "/handler/validataecodehandler.ashx?id=12"/></em></Div>    

ASP. NET Write Verification code

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.