Do your own verification code. net

Source: Internet
Author: User

I learned how to do the verification code today.

The following are simple steps:

1. Create a validate. ashx page and replace the function body of processrequest (httpcontext context) with the following content.

Context. response. contenttype = "image/JPEG ";

Using (system. Drawing. Bitmap bitmap = new system. Drawing. Bitmap (100, 40 ))

{

Using (system. Drawing. Graphics G = system. Drawing. Graphics. fromimage (Bitmap ))

{

Random Rand = new random ();

Int code = Rand. Next (1000,999 9 );

String strcode = code. tostring ();

// If you want the General handler to use the session, the system. Web. sessionstate. irequiressessionstate interface must be implemented.

Httpcontext. Current. session ["code"] = strcode;

G. drawstring (strcode, new system. Drawing. Font ("", 30), system. Drawing. Brushes. Green, new system. Drawing. pointf (0, 0 ));

System. Drawing. Pen = (system. Drawing. Pen) system. Drawing. Pens. Red. Clone ();

G. drawline (pen, new system. Drawing. Point (10, 10), new system. Drawing. Point (20, 20 ));

Bitmap. Save (context. response. outputstream, system. Drawing. imaging. imageformat. JPEG );

}

}

 

The above code mainly uses some gui knowledge, so you don't have to study it in depth. just skip it.

 

2. Create a validate. ASPX page, and add a in <div> </div> to make the src attribute the name of the page created in step 1. If

If you want to click a piece and change the verification code, you can add an onclick event for to redirect it to the page created in step 1.

Onclick = "This. src = 'validate. ashx '"

 

3. Run the page created in step 2 to see the effect. Then add a textbox and a button in validate. aspx. the click of the button is

The response function is as follows:

String txtval = txtvalidate. text;

If (session ["code"]. tostring () = txtval)

{

Response. Write ("Logon successful! ");

}

Else

{

Response. Write ("Logon Failed! ");

}

4. Okay. It's over. You can run the validate. ASPX page to view the effect. Simple? But if you have a try, you will know.

 

5. In fact, there is still a problem above. In The onclick event, we assigned this. SRC to validate. ashx, which was originally intended to request the server again,

The server generates a verification code again. But the key is that the previous page is still validate. ashx. When the server detects that the page is still the original

It does not respond to this page. Therefore, the verification code we have done above still cannot achieve this effect by clicking. There is a way to solve this problem.

When validate. ashx is passed to the server, we add some parameters, but the parameters are different each time. According to this idea, random numbers in JS can be used to generate parameters,

You can also use a time function to solve the problem. I used a time function to solve the problem. The figure is just convenient. Change the onclick event to "This. src = 'validate. ashx?

AAA = '+ new date ()". OK. The final result is displayed.

 

 

 

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.