Create a verification code in the ashx file (use session to inherit irequiressessionstate)

Source: Internet
Author: User

[Network reprint] Note:
You must inherit the system. Web. sessionstate. irequiressessionstate interface to read and write sessions!

System. Web. sessionstate Interfaces

Ireadonlysessionstate specifies the target HTTP ProcessingProgramYou only need to have the read permission on the session status value. This is a tag interface and there is no way.
Irequiressessionstate specifies that the target HTTP handler must have read and write access to the session status value. This is a tag interface and there is no way.

 

------------------------------------------------

Ashx File

 

<% @ Webhandler Language = "C #" class = "checkcode" %>

Using system;
Using system. Web;
Using system. drawing;
Using system. Web. sessionstate;

Public class checkcode: ihttphandler, irequiressessionstate
{

Public void processrequest (httpcontext context)
{
Context. response. contenttype = "text/plain ";
String code = generatecheckcode ();
Context. session ["code"] = code;
This. createcheckcodeimage (Code, context );
}
Private string generatecheckcode ()
{
Int number;
Char code;
String checkcode = string. empty;

System. Random random = new random ();

For (INT I = 0; I <4; I ++)
{
Number = random. Next ();

If (Number % 2 = 0)
Code = (char) ('0' + (char) (Number % 10 ));
Else
Code = (char) ('A' + (char) (Number % 26 ));

Checkcode + = code. tostring ();
}

Return checkcode;
}

Private void createcheckcodeimage (string checkcode, httpcontext context)
{
If (checkcode = NULL | checkcode. Trim () = string. Empty)
Return;

System. Drawing. bitmap image = new system. Drawing. Bitmap (INT) math. Ceiling (checkcode. length * 12.5), 22 );
Graphics G = graphics. fromimage (image );

Try
{
// Generate a random Generator
Random random = new random ();

// Clear the background color of the image
G. Clear (color. White );

// Draw the background noise line of the image
For (INT I = 0; I <5; I ++)
{
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 (color. Silver), X1, Y1, X2, Y2 );
}

Font font = new system. Drawing. Font ("Arial", 12, (system. Drawing. fontstyle. Bold | system. Drawing. fontstyle. italic ));
System. drawing. drawing2d. lineargradientbrush brush = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. blue, color. darkred, 1.2f, true );
G. drawstring (checkcode, Font, brush, 2, 2 );

// Foreground noise of the image
For (INT I = 0; I <100; I ++)
{
Int x = random. Next (image. width );
Int y = random. Next (image. Height );

Image. setpixel (X, Y, color. fromargb (random. Next ()));
}

// Draw the border line of the image
G. drawrectangle (new pen (color. Silver), 0, 0, image. Width-1, image. Height-1 );

System. Io. memorystream MS = new system. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. GIF );
Context. response. clearcontent ();
Context. response. contenttype = "image/GIF ";
Context. response. binarywrite (Ms. toarray ());
}
Finally
{
G. Dispose ();
Image. Dispose ();
}
}
Public bool isreusable {
Get {
Return false;
}
}


}

----------------------------------------------------------------------------

Page Verification

Check whether the verification code is correct
Session ["code"]. tostring () = value of the text box entered by the user

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.