Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. drawing;
Using system. IO;
/// <Summary>
/// Generate a random Verification Code
/// </Summary>
Public partial class productinfo_login_range: system. Web. UI. Page
{
Random random = new random ();
Protected void page_load (Object sender, eventargs E)
{
String STR = getrandomvalidate (4 );
Session. Add ("rangecode", STR); // this step is used to write the verification code to the session for verification. The default setting is not allowed. You can also use cookies.
// Httpcookie cook = new httpcookie ("Code ");
// Cook. value = STR;
Getimagevalidate (STR );
}
// Obtain a random string with its own length defined
Private string getrandomvalidate (INT Len)
{
Int num;
Int TEM;
String rtustr = "";
For (INT I = 0; I <Len; I ++)
{
Num = random. Next ();
If (Num % 2 = 0)
TEM = num % 10 + '0'; // generate a number
Else
TEM = num % 26 + 'a'; // generate characters
Rtustr + = convert. tochar (TEM). tostring ();
}
Return rtustr;
}
// Generate an image
Private void getimagevalidate (string strvalue)
{
// String STR = "oo00"; // the first two are letters O, and the last two are digits 0
Int width = convert. toint32 (strvalue. length * 12 ); // Calculate the Image Width
Bitmap IMG = new Bitmap (width, 23 );
Graphics GFC = graphics. fromimage (IMG ); // Generate a graphics object for drawing
GFC. Clear (color. White );