Simple creation of asp.net verification code and asp.net Verification Code

Source: Internet
Author: User

Simple creation of asp.net verification code and asp.net Verification Code

In fact, there are a lot of articles on the creation of the asp.net verification code, but I want to continue to share with you today. You can combine several examples to compile ASP for your website. NET verification code, which is about two parts:

First create an asp.net form ValidateCode. aspx; Do not write anything. Directly write the following code in the backend ValidateCode. aspx. cs:

Protected void Page_Load (object sender, EventArgs e) {string validateCode = CreateValidateCode (); // generate the verification code Bitmap bitmap = new Bitmap (imgWidth, imgHeight ); // generate Bitmap image DisturbBitmap (bitmap); // image background DrewValidateCode (bitmap, validateCode); // draw the verification code image bitmap. save (Response. outputStream, ImageFormat. gif); // Save the image and wait for the output} private int codeLen = 4; // verification code length private int fineness = 85; // image definition private int imgWidth = 48; // Image Width private int imgHeight = 24; // Image Height private string fontFamily = "Times New Roman"; // font name private int fontSize = 14; // font size // private int fontStyle = 0; // Font Style private int posX = 0; // draw the starting coordinate X private int posY = 0; // draw Y private string CreateValidateCode () // generate verification code {string validateCode = ""; Random random = new Random (); // Random number object for (int I = 0; I <codeLen; I ++) // cyclically generate each value {int n = random. next (10 ); // Number validateCode + = n. ToString ();} Session ["vcode"] = validateCode; // Save the verification code. This Session is called at the front end. Return validateCode; // return verification code} private void DisturbBitmap (Bitmap bitmap) // image background {Random random = new Random (); // generate for (int I = 0; I <bitmap. width; I ++) // generate {for (int j = 0; j <bitmap. height; j ++) {if (random. next (90) <= this. fineness) bitmap. setPixel (I, j, Color. lightGray) ;}} private void DrewValidateCode (Bitmap bitmap, string validateCode) // draw the verification code image {Graphics g = Graphics. fromImage (bitmap); // obtain the Paster object Font font = new Font (fontFamily, fontSize, FontStyle. bold); // set the drawing font g. drawString (validateCode, font, Brushes. black, posX, posY); // draw Verification Code image}

The following functions are implemented on the Login. aspx form page:

Login. aspx form Foreground:

// This function is in the click Verification Code picture will replace the verification code // you can use Microsoft built-in jqury. js under the jquery-1.4.1.min.js version above. You can also download it from the official jquery website. <Script src = "styles/jquery-1.4.1.min.js" type = "text/javascript"> </script> function f_refreshtype () {var Image1 = document. getelementbyidx_x_x ("img"); if (Image1! = Null) {Image1.src = Image1.src + "? ";}}---  // call a function to change the verification code

Background code: click "Log on" to verify that the user is entered correctly.

  string usercode = txtcode.Text.Trim();      if (usercode == Session["vcode"].ToString())//Session["vcode"]      {  }

Other codes are the same as others.

The above is the process of generating an ASP. NET verification code, which we will share with you.

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.