Code writing for verification code using C # ASP
Generally use asynchronous click the foreground verification code picture request once:
Front Desk Picture code:
<id= "Imgvalidatecode" src= ". /ashx/validatecode.ashx " alt=" Click to refresh " style=" vertical-align:middle; " />
Service-side General handler: Validatecode.ashx
Public classashx_validatecode:basehttphandlerno{ Public Override voidProcessRequest (HttpContext context) {Base. ProcessRequest (context); //Settings page is not cachedResponse.Buffer =true; Response.ExpiresAbsolute= System.DateTime.Now.AddSeconds (-1); Response.Expires=0; Response.CacheControl="No-cache"; Response.appendheader ("Pragma","No-cache"); stringCheckcode =validatecode.getrandomcode (4); session["Checkcode"] =Checkcode; Validatecode.createimage (Checkcode,response); }}
Note: Validatecode.getrandomcode (4) is the primary method for obtaining verification codes.
Here's the code to post this Validatecode class:
Public Static stringGetrandomcode (intCodecount) { stringAllchar ="0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,w,x,y,z"; string[] Allchararray = Allchar.split (','); stringRandomcode =""; inttemp =-1; Random Rand=NewRandom (); for(inti =0; i < Codecount; i++) { if(Temp! =-1) {Rand=NewRandom (TEMP * I * ((int) (DateTime.Now.Ticks)) ; } intt = rand. Next ( -); while(temp = =t) {T= Rand. Next ( -); } temp=T; Randomcode+=Allchararray[t]; } returnRandomcode; } Public Static voidCreateImage (stringcheckcode,httpresponse Response) { intIwidth = (int) (Checkcode.length * -); System.Drawing.Bitmap Image=NewSystem.Drawing.Bitmap (iwidth, -); System.Drawing.Graphics g=System.Drawing.Graphics.FromImage (image); System.Drawing.Font F=NewSystem.Drawing.Font ("Arial",Ten);//, System.Drawing.FontStyle.Bold);System.Drawing.Brush B =NewSystem.Drawing.SolidBrush (System.Drawing.Color.Black); System.Drawing.Brush R=NewSystem.Drawing.SolidBrush (System.Drawing.Color.FromArgb (166,8,8)); //G.fillrectangle (New System.Drawing.SolidBrush (Color.Blue), 0, 0, image. Width, image. Height); //g.clear (color.aliceblue);//Background ColorG.clear (System.Drawing.ColorTranslator.FromHtml ("#99C1CB"));//Background Color Char[] ch =Checkcode.tochararray (); for(inti =0; I < Ch. Length; i++) { if(Ch[i] >='0'&& Ch[i] <='9') { //numbers are shown in redg.DrawString (Ch[i]. ToString (), F, R,3+ (I * A),3); } Else { //The letters are shown in blackg.DrawString (Ch[i]. ToString (), F, B,3+ (I * A),3); } } //The For loop is used to generate some random horizontal linesPen Blackpen =NewPen (Color.Black,0); Random Rand=NewRandom (); for(intI=0;i<2; i++) { inty =Rand. Next (image. Height); G.drawline (Blackpen,0, Y,image. Width,y); } System.IO.MemoryStream Ms=NewSystem.IO.MemoryStream (); Image. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); //History back does not repeatResponse. Cache.setnostore ();//this sentenceResponse. ClearContent (); Response. ContentType="Image/jpeg"; Response. BinaryWrite (Ms. ToArray ()); G.dispose (); Image. Dispose (); }
is not very simple ~ the middle does not want the horizontal line to comment out!
C # ASP. NET validation code