This article mainly introduces the creation and use of c#http://www.php.cn/php/php-tp-codes.html "target=" "_blank" > Verification Code, and analyzes the creation of C # Verification code in detail with the example form. Verification and other operating procedures and related skills, the need for friends can refer to the following
This paper describes how to create and use C # verification code. Share to everyone for your reference, as follows:
1. C # Create verification code
① Create Get Verification code page (validatecode.aspx)
② Write get Captcha code (ValidateCode.aspx.cs)
<summary>///Verification Code type (0-alphanumeric mix, 1-digit, 2-letter)///</summary>private string validatecodetype = "0";///< summary>///number of verification code characters///</summary>private int validatecodecount = 4;///<summary>///captcha Character Set, Removed some easily confusing characters///</summary>char[] character = {' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' J ', ' K ', ' L ', ' M ', ' N ', ' P ', ' R ', ' S ', ' T ', ' W ', ' X ', ' Y '};p rotected void Page_Load (object sender, EventArgs e) { Cancel Cache Response.bufferoutput = true; Response.Cache.SetExpires (DateTime.Now.AddMilliseconds (-1)); Response.Cache.SetCacheability (System.Web.HttpCacheability.NoCache); Response.appendheader ("Pragma", "No-cache"); Gets the Set parameter if (!string. IsNullOrEmpty (request.querystring["Validatecodetype")) {Validatecodetype = request.querystring["ValidateCodeType "]; } if (!string. IsNullOrEmpty (request.querystring["Validatecodecount"])) {int. TryParse (request.querystring["Validatecodecount"], out validatecodecount); }//Generate Verification code thiS.createcheckcodeimage (Generatecheckcode ());} private String Generatecheckcode () {char code; string checkcode = String.Empty; System.Random random = new Random (); for (int i = 0; i < Validatecodecount; i++) {code = Character[random. Next (character. Length)]; Requirements are all numbers or letters if (Validatecodetype = = "1") {if ((int) code < 48 | | (int) code > $) {i--; Continue }} else if (Validatecodetype = = "2") {if (int) code < 65 | | (int) code > +) {i--; Continue }} Checkcode + = code; } Response.Cookies.Add (New System.Web.HttpCookie ("Checkcode", Checkcode)); This. session["Checkcode"] = Checkcode; return checkcode;} private void Createcheckcodeimage (string checkcode) {if (Checkcode = = NULL | | Checkcode.trim () = = String.Empty) return ; System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) math.ceiling ((checkcode.length*15.0+40)), 23); System.Drawing.Graphics g = System.Drawing.GraphicS.fromimage (image); try {//Generate random generator randomly random = new random (); Empty the picture background color g.clear (System.Drawing.Color.White); Picture the background noise line for (int i = 0; i < i++) {int x1 = random. Next (image. Width); int x2 = random. Next (image. Width); int y1 = random. Next (image. Height); int y2 = random. Next (image. Height); G.drawline (New System.Drawing.Pen (System.Drawing.Color.Silver), x1, y1, x2, y2); } System.Drawing.Font Font = new System.Drawing.Font ("Arial", +, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (new System.Drawing.Rectangle (0, 0, image. Width, image. Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true); int cyspace = 16; for (int i = 0; i < Validatecodecount; i++) {g.drawstring (checkcode.substring (i, 1), Font, brush, (i + 1) * cy Space, 1); }//Picture The foreground noise point for (int i = 0; I < 100; i++) {int x = random. Next (image. Width); int y = random. Next (image. Height); Image. SetPixel (x, Y, System.Drawing.Color.FromArgb (random. Next ())); }//Draw the border line of the picture G.drawrectangle (new System.Drawing.Pen (System.Drawing.Color.Silver), 0, 0, image. Width-1, image. HEIGHT-1); System.IO.MemoryStream ms = new System.IO.MemoryStream (); Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF); Response.clearcontent (); Response.ContentType = "Image/gif"; Response.BinaryWrite (Ms. ToArray ()); } finally {g.dispose (); Image. Dispose (); }}
2, the use of verification code
① code for the front section of the Verification Code
The code is as follows:
② Creating a Verification Code test page (validatetest.aspx)
③ Writing Verification Code test submission codes (ValidateTest.aspx.cs)
protected void Btnval_click (object sender, EventArgs e) {bool result = false; Validation result string usercode = This.txtValidate.Value; Obtain the user input verification code if (String.IsNullOrEmpty (Usercode)) {//Enter the verification code return; } string validcode = this. session["Checkcode"] as String; Gets the system-generated verification code if (!string. IsNullOrEmpty (Validcode)) {if (usercode.tolower () = = Validcode.tolower ()) {//Verify success result = true; } else {//validation failed result = false; } }}