Accumulated [C #] --- verification code, ajax submission,
Effect:
Ideas:
Use the ashx file to create a four-digit verification. First, four random numbers are generated. Create a canvas, store the created verification code to the session, and then perform the button on the foreground to send an ajax request to the background for the value in the text box, and compare it with the verification code in the session. true is returned successfully, false is returned for failure.
Code:
Front-end]
1 <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "verifycodeDemo. aspx. cs" Inherits = "verifycodeDemo. verifycodeDemo" %> 2 3 <! DOCTYPE html> 4 5
[Background]
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 using System. web. UI; 6 using System. web. UI. webControls; 7 8 namespace verifycodeDemo 9 {10 public partial class verifycodeDemo: System. web. UI. page11 {12 protected void Page_Load (object sender, EventArgs e) 13 {14 string action = Request. params ["action"]; 15 string VerifyCodeValue = Request. params ["VerifyCo De "]; 16 if (action =" comparison ") 17 {18 string Msg =" true "; 19 // compare the verification code stored in the session 20 if (HttpContext. current. session ["dt_session_code"] = null | VerifyCodeValue. toLower ()! = HttpContext. current. session ["dt_session_code"]. toString (). toLower () 21 {22 Msg = "false"; // Incorrect verification code input 23} 24 Response. write (Msg); 25 Response. end (); 26} 27 28} 29} 30}
[Ashx file]
1 using System; 2 using System. collections. generic; 3 using System. drawing; 4 using System. drawing. imaging; 5 using System. IO; 6 using System. linq; 7 using System. web; 8 using System. web. sessionState; 9 10 11 namespace ESoftMS. web. frame 12 {13 /// <summary> 14 /// VerifyCode abstract description qingapple (www.cnblogs.com/xinchun) 15 /// </summary> 16 public class VerifyCode: IHttpHandler, IRequiresSessionState 17 {18 19 public void ProcessRequest (HttpContext context) 20 {21 int codeW = 80; 22 int codeH = 22; 23 int fontSize = 16; 24 string chkCode = string. empty; 25 // Color list for verification code, noise line, and noise 26 color [] Color = {Color. black, Color. red, Color. blue, Color. green, Color. orange, Color. brown, Color. brown, Color. darkBlue}; 27 // font list for Verification Code 28 string [] font = {"Times New Roman", "Verdana", "Arial", "Gungsuh ", "Impact"}; 29 // character Set of the Verification Code, removing some confusing characters 30 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'}; 31 Random rnd = new Random (); 32 // generate the verification code string 33 for (int I = 0; I <4; I ++) 34 {35 chkCode + = character [rnd. next (character. length)]; 36} 37 // write Session 38 context. session ["dt_session_code"] = chkCode; 39 // create a canvas 40 Bitmap bmp = new Bitmap (codeW, codeH); 41 Graphics g = Graphics. fromImage (bmp); 42g. clear (Color. white); 43 // draw noise line 44 for (int I = 0; I <1; I ++) 45 {46 int x1 = rnd. next (codeW); 47 int y1 = rnd. next (codeH); 48 int x2 = rnd. next (codeW); 49 int y2 = rnd. next (codeH); 50 Color clr = color [rnd. next (color. length)]; 51g. drawLine (new Pen (clr), x1, y1, x2, y2); 52} 53 // draw the verification code string 54 for (int I = 0; I <chkCode. length; I ++) 55 {56 string fnt = font [rnd. next (font. length)]; 57 Font ft = new Font (fnt, fontSize); 58 Color clr = color [rnd. next (color. length)]; 59g. drawString (chkCode [I]. toString (), ft, new SolidBrush (clr), (float) I * 18 + 2, (float) 0 ); 60} 61 // Drawing Noise 62 // for (int I = 0; I <1; I ++) 63 // {64 // int x = rnd. next (bmp. width); 65 // int y = rnd. next (bmp. height); 66 // Color clr = color [rnd. next (color. length)]; 67 // bmp. setPixel (x, y, clr); 68 //} 69 // clear the output cache of the page and set the page to no cache 70 context. response. buffer = true; 71 context. response. expiresAbsolute = System. dateTime. now. addMilliseconds (0); 72 context. response. expires = 0; 73 context. response. cacheControl = "no-cache"; 74 context. response. appendHeader ("Pragma", "No-Cache"); 75 // write the verification code image to the memory stream, and output 76 MemoryStream MS = new MemoryStream (); 77 try 78 {79 bmp in "image/Png" format. save (MS, ImageFormat. png); 80 context. response. clearContent (); 81 context. response. contentType = "image/Gif"; 82 context. response. binaryWrite (ms. toArray (); 83} 84 catch (Exception) 85 {86 87} 88 finally 89 {90g. dispose (); 91 bmp. dispose (); 92} 93} 94 95 public bool IsReusable 96 {97 get 98 {99 return false; 100} 101} 102}
Download Demo:
Http://files.cnblogs.com/xinchun/verifycodeDemo.rar
Make a word
The accumulation of something should be
Is "force"
What do you mean when I'm tired of "Inch?
Synonyms accumulate less and more, collect more and more, and the opposite of the aggregate tower is exposed to extreme cold
C branch n j branch zh Branch l branch I) Explanation of the baht: Ancient measurement unit, 24 baht for one or two (1 kg = 16 two); tired: accumulation.
Describe the accumulation