Asp.net Random verification code (using ashx)

Source: Internet
Author: User

Validatecode. ashx

<% @ Webhandler Language = "C #" class = "validatecode" %> using system; using system. data; using system. web; using system. collections; using system. web. services; using system. web. services. protocols; using system. drawing;

Public class validatecode: ihttphandler {

Public void processrequest (httpcontext context) {context. response. cache. setcacheability (httpcacheability. nocache); // pay special attention. If this parameter is not added, click Verify image 'do not see another login. this. createcheckcodeimage (generatecheckcode (context), context );}

Public bool isreusable {get {return false ;}} private string generatecheckcode (httpcontext context) {int number; char code; string checkcode = string. empty;

System. Random random = new random ();

for (INT I = 0; I <6; I ++) {number = random. Next ();

If (Number % 2 = 0) code = (char) ('0' + (char) (Number % 10 )); else if (Number % 3 = 0) code = (char) (''a' + (char) (Number % 26); else code = (char) (''a' + (char) (Number % 26); checkcode + = code. tostring () ;}

Context. response. cookies. add (New httpcookie ("checkcode", checkcode); // It can also be stored in seesion. // context. session ["checkcode"] = checkcode; 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 );

& Nbs

P; try {// generate the random generator random = new random ();

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

// specifies the background noise line of the image. For (INT I = 0; I <25; 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 G of the image. 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 & nbs

P; {G. Dispose (); image. Dispose ();}

}}

Page:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">

R/> </form> </body>

BackgroundCode:

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;

Public partial class default: system. Web. UI. Page {protected void page_load (Object sender, eventargs e ){

}

protected void button#click (Object sender, eventargs e) {If (request. cookies ["checkcode"] = NULL) {lblmessage. TEXT = "your browser settings have been disabled. You must set the options that allow the browser to use cookies before using the system. "; Lblmessage. Visible = true; return ;}

If (string. Compare (request. Cookies ["checkcode"]. Value, txtcheckcode. Text, true )! = 0) {lblmessage. Text = "Incorrect verification code. Enter the correct verification code. "; Lblmessage. Visible = true; return ;}

// Execute the next step here // checkcode is saved to the session // If (session ["checkcode"]! = NULL) // {// If (string. Compare (session ["checkcode"]. tostring (), txtcheckcode. Text. Trim (), true )! = 0) // {// lblmessage. Text = "the verification code is incorrect. Enter the correct verification code. "; // Lblmessage. Visible = true; // return; //} // else // {// execute the next step here //}//}}}

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.