Asp.net generates a graphical verification code Static Graphic Verification Code

Source: Internet
Author: User

The graphic verification code is a common method for anti-injection or security verification in WEB development. I will introduce you to the usage of the graphic verification code and Static Graphic verification code generated by asp.net.

Spend less time on code

Create a general handler... The. ashx file is as follows: CreateCode. ashx

The Code is as follows: Copy code
<% @ WebHandler Language = "C #" Class = "CreateCode" %>

Using System;
Using System. Web;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Web. SessionState;
Using System. Collections. Generic;
Using System. Text;
Using System. Drawing;

Public class CreateCode: IHttpHandler, IRequiresSessionState
{
// Verification code by Jack
Public void ProcessRequest (HttpContext context)
{
System. Web. HttpResponse response = context. Response;
// Text content of the Verification Code
String checkCode = CreateCheckCodeString ();
Context. Session. Add ("createcodeStr", checkCode );

Int width = 80; // The image width of the verification code.
Int height = 22; // The Image height of the Verification Code
Font font = new Font ("Arial", 12, FontStyle. Bold); // the Font of the verification code.
SolidBrush brush = new SolidBrush (Color. Black); // The brush used to write the verification code
Pen crosswise = new Pen (Color. Green, 0); // draw a Pen with horizontal interference lines
Pen vertical = new Pen (Color. FromArgb (255,100,100,100), 0); // draw a Pen with vertical interference lines
Bitmap image = new Bitmap (width, height); // generate an image
Graphics g = Graphics. FromImage (image); // generate a painting Panel (canvas)
G. Clear (ColorTranslator. FromHtml ("# f0f0f0"); // fill the canvas with the specified color
RectangleF rect = new RectangleF (5, 2, width, height); // defines the drawing rectangle of the text.
Random rand = new Random (int) DateTime. Now. Ticks); // generate Random object of interference line
For (int I = 0; I <2; I ++)
{
Point start = new Point (0, rand. Next (height ));
Point end = new Point (width, rand. Next (height ));
G. DrawLine (crosswise, start, end );
}
For (int I = 0; I <4; I ++)
{
Point start = new Point (rand. Next (width), 0 );
Point end = new Point (rand. Next (width), height );
G. DrawLine (vertical, start, end );
}
G. DrawString (checkCode, font, brush, rect); // write the verification code to the canvas.
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Try
{
Image. Save (MS, System. Drawing. Imaging. ImageFormat. Gif );
Response. ClearContent ();
Response. ContentType = "image/Gif ";
Response. BinaryWrite (ms. ToArray ());
}
Finally
{
Ms. Flush ();
Ms. Close ();
Ms. Dispose ();
G. Dispose ();
Image. Dispose ();
}
}
Private string CreateCheckCodeString ()
{// Define the character array used for verification code
Char [] AllCheckCodeArray = {'1', '2', '3', '4', '5', '6', '7', '8 ', '9', 'A', 'B', 'C ',
'D', 'E', 'F', 'G', 'h', 'I', 'J', 'k', 'l', 'M ', 'N', 'O', 'P', 'Q', 'R', 's', 't', 'U', 'V', 'w ',
'X', 'y', 'z '};
// Define the verification code string
String randomcode = "";
Random rd = new Random ();
// Generate a four-digit verification code string
For (int I = 0; I <5; I ++)
Randomcode + = AllCheckCodeArray [rd. Next (AllCheckCodeArray. Length)];
Return randomcode;
}
Public bool IsReusable
{
Get
{
Return false;
}
}

}

The five verification codes are stored in the session to verify the createcodeStr

Page reference

The Code is as follows: Copy code

Style = "cursor: pointer;" runat = "server" title = "click to refresh the verification code" alt = "click to refresh the verification code"/>


Click here to change the verification code.

During background verification.

The Code is as follows: Copy code

String dd = HttpContext. Current. Session ["Mind_createcodeStr"]. ToString ();
If (dd. Equals (validatCode. ToUpper ())){}

Here, validatCode is the verification code that is received by the user.

Or the user directly
 

Of course, the server control of runat = "server" is obtained in the background, or the html form is submitted to the background request object for retrieval.

I believe that Tom can understand... is it too cool?

I suddenly thought something ....


This is the second ....

The Code is as follows: Copy code

<% @ WebHandler Language = "C #" Class = "CreateCode" %>

Using System;
Using System. Web;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. Web. SessionState;
Using System. Collections. Generic;
Using System. Text;
Using System. Drawing;

Public class CreateCode: IHttpHandler, IRequiresSessionState
{
/// <Summary>
/// Dynamic verification code by Jack
/// Calculate the verification code when the value type is not null
/// </Summary>
Public void ProcessRequest (HttpContext context)
{
System. Web. HttpResponse response = context. Response;
// Text content of the Verification Code
String type = "";
If (context. Request ["type"]! = Null)
Type = context. Request ["type"]. ToString ();
String checkCode = "";
If (string. IsNullOrEmpty (type ))
{
CheckCode = CreateCheckCodeString ();
Context. Session. Add ("gs_codeStr", checkCode );
}
Else
{
String [] codes = CreateCheckCode ();
CheckCode = codes [0];
Context. Session. Add ("gs_codeStr", codes [1]);
}
Int width = 80; // The image width of the verification code.
Int height = 22; // The Image height of the Verification Code
Font font = new Font ("Arial", 12, FontStyle. Bold); // the Font of the verification code.
SolidBrush brush = new SolidBrush (Color. Black); // The brush used to write the verification code
Pen crosswise = new Pen (Color. Green, 0); // draw a Pen with horizontal interference lines
Pen vertical = new Pen (Color. FromArgb (255,100,100,100), 0); // draw a Pen with vertical interference lines
Bitmap image = new Bitmap (width, height); // generate an image
Graphics g = Graphics. FromImage (image); // generate a painting Panel (canvas)
G. Clear (ColorTranslator. FromHtml ("# f0f0f0"); // fill the canvas with the specified color
RectangleF rect = new RectangleF (10, 2, width, height); // define the rectangle for text painting floating x, y, width, height,
Random rand = new Random (int) DateTime. Now. Ticks); // generate Random object of interference line
For (int I = 0; I <2; I ++)
{
Point start = new Point (0, rand. Next (height ));
Point end = new Point (width, rand. Next (height ));
G. DrawLine (crosswise, start, end );
}
For (int I = 0; I <4; I ++)
{
Point start = new Point (rand. Next (width), 0 );
Point end = new Point (rand. Next (width), height );
G. DrawLine (vertical, start, end );
}
G. DrawString (checkCode, font, brush, rect); // write the verification code to the canvas.
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Try
{
Image. Save (MS, System. Drawing. Imaging. ImageFormat. Gif );
Response. ClearContent ();
Response. ContentType = "image/Gif ";
Response. BinaryWrite (ms. ToArray ());
}
Finally
{
Ms. Flush ();
Ms. Close ();
Ms. Dispose ();
G. Dispose ();
Image. Dispose ();
}
}
// Define the string Verification Code
Private string CreateCheckCodeString ()
{// Define the character array used for verification code
Char [] AllCheckCodeArray = {'1', '2', '3', '4', '5', '6', '7', '8 ', '9', 'A', 'B', 'C ',
'D', 'E', 'F', 'G', 'h', 'I', 'J', 'k', 'l', 'M ', 'N', 'O', 'P', 'Q', 'R', 's', 't', 'U', 'V', 'w ',
'X', 'y', 'z '};
// Define the verification code string
String randomcode = "";
Random rd = new Random ();
// Generate a four-digit verification code string
For (int I = 0; I <5; I ++)
Randomcode + = AllCheckCodeArray [rd. Next (AllCheckCodeArray. Length)];
Return randomcode;
}

// Define the number operation verification code {1 + 1 =? , 2}
Private string [] CreateCheckCode ()
{// Define the character array used for verification code
Int [] AllCheckCodeArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Define the verification code string
String randomcode = ""; // 1 + 1 =?
Int result = 0; // 2
Random rd = new Random ();
// Generate a four-digit verification code string
For (int I = 0; I <5; I ++)
If (I! = 1 & I! = 4)
{
Int curint = AllCheckCodeArray [rd. Next (AllCheckCodeArray. Length)];
If (I = 0 | I = 2)
Result + = curint;
If (I = 3)
Randomcode + = "= ";
Else
Randomcode + = curint;
}
Else
{
If (I = 1)
Randomcode + = "+ ";
If (I = 4)
Randomcode + = "? ";
}
Return new string [] {randomcode, result. ToString ()};
}

Public bool IsReusable
{
Get
{
Return false;
}
}

}

I believe I have read the first verification code text I sent. I have a new understanding after reading this second article.

Simple verification code calculation ....

Reference

The Code is as follows: Copy code


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.