Implementation of the login verification code, login verification code

Source: Internet
Author: User

Implementation of the login verification code, login verification code

Front-end labels and methods:
Verification Code:Copy codeThe Code is as follows: <input id = "txtVerifyCode" type = "text" maxlength = "5" style = "line-height: 30px; height: 30px; width: 80px; border: solid 1px # d4d4d4; "class =" input "/> click to refresh the image </p> // tag

$ (Function () {$ ("# imgValidateCode "). click (function () {DoFresh () ;}); DoFresh () ;}) function DoFresh () {var img =$ ("# imgValidateCode"); img. attr ("src", "VerifyCode. aspx? Random = "+ Math. random ();} // Add method. src is the aspx address of the generated image.

Create a VerifyCode. aspx in the project. The following is the aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="VerifyCode.aspx.cs" Inherits="Form.VerifyCode" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

The following is the aspx. cs code:

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. drawing; namespace Form {public partial class VerifyCode: System. web. UI. page {public static string HZ; // <summary> // Maximum length of the Verification Code /// </summary> public int MaxLength {get {return 10 ;}} /// <summary> /// minimum length of the Verification Code /// </summary> public int MinLength {Get {return 1 ;}} protected void Page_Load (object sender, EventArgs e) {string [] str = CreateValidateNumber (4); string strcode = string. empty; for (int I = 0; I <str. length; I ++) {strcode + = str [I];} CreateCheckCodeImage (str); HZ = strcode; Response. write (HZ); // The verification code is saved to the session ["ValidateCode"] = HZ ;} /// <summary> /// generate the verification code /// </summary> /// <param name = "length"> specify the verification code length </param> /// <Returns> Verification Code </returns> public string [] CreateValidateNumber (int length) {string Vchar = "1, 2, 4, 5, 6, 7, 8, 9, a, B, c, d, e, f, g, h, I, j, k, l, m, n, p "+", q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, P, Q "+", R, S, T, U, V, W, X, Y, Z "; string [] VcArray = Vchar. split (new Char [] {','}); // Split it into an array string [] num = new string [length]; int temp =-1; // record the last Random value and avoid producing several identical Random numbers, such as Random rand = new Random (); // use a simple algorithm to ensure generation For (int I = 1; I <length + 1; I ++) {if (temp! =-1) {rand = new Random (I * temp * unchecked (int) DateTime. now. ticks);} int t = rand. next (VcArray. length-1); if (temp! =-1 & temp = t) {return CreateValidateNumber (length);} temp = t; num [I-1] = VcArray [t]; // num. setValue (VcArray [t]); // VNum + = VcArray [t];} return num;} private void CreateCheckCodeImage (string [] checkCode) {if (checkCode = null | checkCode. length <= 0) return; System. drawing. bitmap image = new System. drawing. bitmap (int) Math. ceiling (checkCode. length * 32.5), 60); System. drawing. graphics g = Graphics. fromImage (image); try {// generate Random generator random Random = new Random (); // clear the image background color g. clear (Color. white); // define the Color [] c = {Color. black, Color. red, Color. darkBlue, Color. green, Color. orange, Color. brown, Color. darkCyan, Color. purple}; // the background noise line of the picture for (int I = 0; I <25; I ++) {int cindex = random. next (7); int findex = random. next (5); 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 Pen (c [cindex]), x1, y1, x2, y2);} // defines the font string [] f = {"Verdana ", "Microsoft Sans Serif", "Comic Sans MS", "Arial", ""}; for (int k = 0; k <= checkCode. length-1; k ++) {int cindex = random. next (7); int findex = random. next (5); Font drawFont = new Font (f [findex], 26, (System. drawing. fontStyle. bold); SolidBrush drawBrush = new SolidBrush (c [cindex]); float x = 5.0F; float y = 0.0F; float width = 42.0F; float height = 48366f; int sjx = random. next (10); int sjy = random. next (image. height-(int) height); RectangleF drawRect = new RectangleF (x + sjx + (k * 25), y + sjy, width, height ); stringFormat drawFormat = new StringFormat (); drawFormat. alignment = StringAlignment. center; g. drawString (checkCode [k], drawFont, drawBrush, drawRect, drawFormat);} // foreground noise of the picture for (int I = 0; I <500; I ++) {int x = random. next (image. width); int y = random. next (image. height); image. setPixel (x, y, Color. fromArgb (random. next ();} int cindex1 = random. next (7); // draw the border line g of the image. drawRectangle (new Pen (c [cindex1]), 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 ();}}}}

So! You can generate the verification code, and then write the judgment logic of the verification version!

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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: 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.