The verification code of the letter and man combination, including the function of changing the character

Source: Internet
Author: User

1. Write a verification code. The front-end does not need to write anything. This is the background code of createverificationcode_page.aspx.cs.

Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. drawing;

Namespace Verification Code
{
Public partial class createverificationcode_page: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Place user code here to initialize the page
Createcheckcodeimage (generatecheckcode (5); // charnum = 5,
Session ["checkcode"] = generatecheckcode (5 );
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
// Codegen: This call is required by the ASP. NET web form designer.
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new eventhandler (page_load );
}
# Endregion

Private string generatecheckcode (INT charnum) // charnum is the number of letters or numbers to be displayed in the verification code
{
Int number;
Char code;
String checkcode = string. empty;
Random random = new random ();
For (INT I = 0; I <charnum; I ++)
{
Number = random. Next ();
If (Number % 2 = 0) // convert it to an ascii code to get the added number and then convert it to a char
{
Int x = Number % 10;
If (x = 0 | x = 1)
{
Code = (char) ('0' + (char) (3 ));
}
Else
{
Code = (char) ('0' + (char) (Number % 10 ));
}
}
Else
{
Int y = Number % 26;
If (y = 8 | Y = 14)
{
Code = (char) ('A' + (char) (0 ));
}
Else
{
Code = (char) ('A' + (char) (Number % 26 ));
}
}
Checkcode + = code. tostring ();
}
// Response. Cookies. Add (New httpcookie ("checkcode", checkcode ));
Return checkcode;
}

Private void createcheckcodeimage (string checkcode)
{
If (checkcode = NULL | checkcode. Trim () = string. Empty)
{
Return;
}
System. drawing. bitmap image = new system. drawing. bitmap (INT) math. ceiling (checkcode. length * 12.5), 20); graphics G = graphics. fromimage (image );
Try
{
Random random = new random (); // generate a random Generator
G. Clear (color. White); // clear the background color of the image.
For (INT I = 0; I <2; I ++) // draws the background noise line of the image.
{
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. Black), X1, Y1, X2, Y2 );
}
Font font = new system. Drawing. Font ("Arial", 12, (system. Drawing. fontstyle. Bold ));
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 );
For (INT I = 0; I <100; I ++) // foreground noise of the picture
{
Int x = random. Next (image. width );
Int y = random. Next (image. Height );
Image. setpixel (X, Y, color. fromargb (random. Next ()));
}
G. drawrectangle (new pen (color. Silver), 0, 0, image. Width-1, image. Height-1); // draw the border line of the image
System. Io. memorystream MS = new system. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. GIF );
Response. clearcontent ();
Response. contenttype = "image/GIF ";
Response. binarywrite (Ms. toarray ());
}
Finally
{
G. Dispose ();
Image. Dispose ();
}
}

}
}

2. Verification Code Page

(1) login_page.aspx front-end

<Script language = "JavaScript"> // you can't see another implementation.
Function Change ()
{
VaR IMG = Document. getelementbyid ("imgcode ");
IMG. src = IMG. SRC + '? ';
}
</SCRIPT>

<Tr>
<TD align = "right"> verificationcode: </TD>
<TD> <input type = "text" runat = "server" id = "txtcode" style = "width: 128px" maxlength = "4"/> </TD>
<TD>
<Table>
<Tr>
<TD>

</TD>
<TD>
<A href = "javascript: Change ();" style = "font-size: 12px"> notclear? </A> // you can't see another implementation.
</TD>
</Tr>

(2) login_page.aspx.cs background

Protected void btnlogin_click (Object sender, eventargs e) // click Login to verify if the entered verification code is incorrect
{
String checkcode = session ["checkcode"]. tostring ();
If (txtcode. value. Equals (checkcode, stringcomparison. ordinalignorecase) // you can check whether the input is equal to the Verification code.
{
Response. Write ("<SCRIPT> alert ('verification code input correct! ') </SCRIPT> ");
}
Else
{
Page. clientscript. registerclientscriptblock (clientscript. GetType (), "myscript", "<SCRIPT> alert ('verification code input incorrect! ') </SCRIPT> ");
}
}

 

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.