Joke Collection Website - Public benefit messages - SMS code of mobile phone website

SMS code of mobile phone website

When designing user login module, we often use verification code, which can effectively prevent malicious cracking of hacker software. Now open the source code of my commonly used verification code.

How to use:

1. Add a class in the Web project, such as "CreateImage.cs", and then copy the source code I published;

2. Create a new Web form, such as "Image.aspx", and add the code "CreateImage". DrawImage () "to Page_Load. Of course, don't forget to add a reference to this class! !

3. You can add the following javascript code to the appropriate position on the page (where you want to put the verification code).

The source code is as follows:

[csharp] View plain text

///

///Verification code module

///

Public class CreateImage

{

Public static void DrawImage ()

{

create image img = new create image();

HttpContext。 current . session【“check code“】= imgrnd num(4);

img。 Create an image (HttpContext. Current session ["CheckCode"]. ToString());

}

///

///Generate a verification picture

///

///Verification character

Private void CreateImages (string check code)

{

int iwidth =(int)(checkCode。 Length *13);

System. Drawing. Bitmap image = new system. drawing . Bitmap(iwidth,23);

Graph g = graph. FromImage;

G. transparent (color. White);

//Define color

Color【】c = { Color。 Black, color. Red, color. Dark blue, color. Green, color. Orange, color. Brown, color. Dark cyan, color. Purple};

//Define the font

string【】font = {“verdana“、“Microsoft sans serif“、“Comic sans ms“、“Arial“、“Song Ti“};

Random rand = new Random();

//Random output noise

for(int I = 0; I & lt50; i++)

{

Int x = rand. Next (picture. Width);

Int y = rand. Next (picture. Height);

G.DrawRectangle (new pen (color. Light gray, 0), x, y, 1,1);

}

//Output verification code characters with different fonts and colors.

for(int I = 0; I

{

int cindex = rand。 Next (7);

int findex = rand。 Next (5);

Font f = new system. Drawing. Font (font [findex], 10, system. drawing . font style . bold);

Brush b = new system. drawing . solid brush(c【cindex】);

int ii = 4;

if((I+ 1)% 2 = = 0)

{

ii = 2;

}

G. Pull the rope (check code. substring(I, 1),f,b,3+(I * 12),ii);

}

//Draw a border

G.DrawRectangle (new pen (color. Black, 0), 0, 0 image. Width-1, image. Height-1);

//output to browser

System. IO. memory flow ms = new system. IO . memory stream();

Image. Save (ms, system. drawing . imaging . imageformat . JPEG);

HttpContext。 current . response . clear content();

//response. clear content();

HttpContext。 current . response . content type =“image/Jpeg“;

HttpContext。 current . response . binary write(ms . ToArray());

g.dispose();

Image. dispose();

}

///

///Generate random letters

///

///Number of letters generated

///String

Private string rnd num(int VcodeNum)

{

string Vchar =“0, 1,2,3,4,5,6,7,8,9“;

string【】VC array = Vchar。 Split (',');

String vnum = ""; //StringBuilder is not needed because the string is short.

int temp =- 1; //Record the last random number and try to avoid generating several identical random numbers.

//Adopt a simple algorithm to ensure the difference of generated random numbers.

Random rand = new Random();

for(int I = 1; I & ltvcodenum+1; i++)

{

if(temp! = - 1)

{

Rand = new Random(I * temp * unchecked ((int)DateTime. now . Ticks));

}

Int t = rand. Next (VcArray. Length);

if(temp! =- 1 & amp; & temperature == t)

{

Returns rndnum (vcodenum);

}

temp = t;

VNum+= VC array【t】;

}

Return to VNum

}

}