Design the user login image Verification Code

Source: Internet
Author: User

Steps:
1. The server randomly generates a verification code string with a length of n (n value can be set by the Program). This string can contain numbers, uppercase and lowercase letters, and so on.
2. Create an image to display the verification code string.
3. display the verification string on the Image
When a user refreshes the page, the strings created each time are different. On the browser side, the user inputs the strings on the image and submits them to the server, compare the string submitted by the user with the string saved on the server to verify whether it is consistent. If it is consistent, continue; otherwise, a prompt message is returned.

Eg.
// Create a random number
Private int getrandomint (INT Imin, int IMAX)
{
Random random = new random ();
Return (random. Next (Imin, IMAX );
}

// Create all characters to prepare for creating a verification string
Private void initletterlist ()
{
For (INT I = 0; I <10; I ++)
{
// Add a number
Letterlist. append (I. tostring ());
}

For (INT I = 0; I <26; I ++)
{
// Add uppercase characters
Letterlist. append (char) (INT) 'A' + I). tostring ());
}

For (INT I = 0; I <26; I ++)
{
// Add lowercase characters
Letterlist. append (char () (INT) 'A' + I). tostring ());
}

}

// Create a verification string
Private string createvalidatestring (INT nlen)
{
// Initialization
Initletterlist ();
// Create a stringbuilder () object
Stringbuilder sb = new stringbuilder (nlen );
For (INT I = 0; I <nlen; I ++)
{
// Generate a random character
Int Index = getrandomint (0, letterlist. Length-1 );
SB. append (letterlist [Index]. tostring ());
Letterlist. Remove (index, 1 );
}
// Return the verification code string
Return (sb. tostring ());

}

// To make the verification code string unrecognizable, The fontstyle attribute and the brush attribute of the string are randomly set when the verification code string is output.

// Obtain the enumerated values of the fontsytle Attribute Based on the random number generation
Private int createrandomfontstyle (INT random)
{
If (random <200) {return 0 };
If (random <400) {return 1 };
If (random <600) {return 2 };
If (random <800) {return 3 };
If (random <1000) {return 4 };
Return 4;
}

// Initialize the brush list
Private void initbrushlist ()
{
// Add a brush
Brushlist [0] = systembrushes. activeborder;
....
Brushlist [31] = systembrushes. windowtext;
}

// Create and display the verification code string on the page
1. Call initbrushlist () to initialize and save the brushlist array.
2. Obtain the verification code string generated by the server.
3. Create a BMP bitmap
4. output the verification code string on the BMP bitmap.
5. Set the output format and output the page
Private readonly string ImagePath = "images/validator.jpg ";
Private string svalidator = "";
Private brush [] brushlist = new brush [32];

Pageload Method
{
// Initialization
Initbrushlist ();
If (request. Params ["validator"]! = NULL)
{
// Obtain the verification string
Svalidator = request. Params ["validator"]. tostring ();
}
// Create a BMP bitmap
Bitmap bitmapimage = new system. Drawing. Bitmap (server. mappath (ImagePath );
Graphics graphicimage = graphics. fromimage (bitmapimage );

/// Set the paint brush output mode
Graphicimage. smoothingmode = smoothingmode. antialias;
/// Add a text string
For (INT I = 0; I <svalidator. length; I ++)
{
Graphicimage. drawstring (svalidator [I]. tostring (),
New font ("Arial", 20, (fontstyle) createrandomfontstyle (getrandomint (0,1000 ))),
Brushlist [getrandomint (0, brushlist. Length-1)],
New pointf (I * 15, getrandomint (-5, 5 )));
}

// Graphicimage. drawstring (svalidator, new font ("Arial", 20, (fontstyle) getrandomint (0, 4), systembrushes. windowtext, new point (0, 0 ));

/// Set the image output format
Response. contenttype = "image/JPEG ";

/// Save the data stream
Bitmapimage. Save (response. outputstream, imageformat. JPEG );

/// Release the occupied Resources
Graphicimage. Dispose ();
Bitmapimage. Dispose ();
Response. End ();
}

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.