ASP. NET login interface verification code implementation

Source: Internet
Author: User

Recently, I was working on a system. I found a project in my junior year at school. I found something that can be used, for example, the implementation of the login interface verification code. The purpose of the verification code design is to prevent malicious logon. The following can be done in ASP:

1. Create a login page with a verification code input box added, followed by an image. The source address of the image is An ASPX page that generates the verification code.

 
Verification Code: <asp: textbox id = "txtcheckcode" runat = "server" font-names = "" font-size = "medium" width = "113px"> </ASP: textbox> <asp: Image id = "image4" runat = "server" imageurl = "~ /Checkcode. aspx "/>

2. Create the checkcode. ASPX page. You only need to add a vertex between the head flag.Code

 
<% Response. buffer = true; %> <% response. expiresabsolute = datetime. now. addseconds (-1); %> <% response. expires = 0; %> <% response. cachecontrol = "no-Cache"; %>

3. Add code to the checkcode. aspx. CS page.

First, add a user space:

Using system. Data. sqlclient; using system. drawing;

Add the remaining code:

Protected void page_load (Object sender, eventargs e) {createcheckcodeimage (generatecheckcode () ;}// create a verification code private string generatecheckcode () {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 {code = (char) ('A' + (char) (Number % 26);} checkcode + = code. tostring ();} session ["checkcode"] = checkcode; return checkcode;} // generate the verification code background image private void createcheckcodeimage (string checkcode) {If (checkcode = NULL | checkcode. trim () = string. empty) return; system. drawing. bitmap image = new Bitmap (INT) math. ceiling (checkcode. length * 13.5), 22); graphics G = graphics. fromimage (image); try {// generate random generator random = new random (); // clear the image background color G. clear (color. white); // after all, the noise line 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, x2, Y1, Y2);} Font font = new font ("Arial", 12, (fontstyle. bold | 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 to be painted 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); response. clearcontent (); response. contenttype = "image/GIF"; response. binarywrite (Ms. toarray ();} finally {G. dispose (); image. dispose ();}}

At the beginning, this login module was made by a girl. Although I made the core module of the system at that time, and she only made a login module, the final score of others was higher than that of me, my family scored 95, But I scored 90, which is too embarrassing.

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.