ASP. How to use verification code in net

Source: Internet
Author: User

Main function:

* Generate Verification Code

* Validation Code

Basic principle:

Generates a random 5 character (consisting of 0-9 digits and a-Z letter) According to certain rules and writes the session. When validation is done, the comparison is taken out from the session.

Prerequisite Knowledge:

About ASHX files

Essence: An ASP tutorial x file that lacks HTML files.

Usage scenarios:

Suitable for generating dynamic images or text.

ASHX output as the background of the page element img (attribute src value, eg:)

There is a drawback to the. ashx file, which is cumbersome to handle postback events for controls, such as if you use it to generate a list of DataGrid, but processing the postback of the data requires some. aspx page functionality that only handles these features manually. Therefore, the general use of. ASHX, to output some items do not need postback processing.

Programming

1 <%@ webhandler language= "C #" class= "watermark"%>

2

3 using System;

4 using System.Web;

5 using System.Drawing;

6 using System.Drawing.Drawing2D;

7 using System.Web.SessionState;

8

9 public class watermark:ihttphandler,irequiressessionstate{

10//Use session must implement IRequiresSessionState interface, and introduce namespaces System.Web.SessionState

One public void ProcessRequest (HttpContext context) {

A string checkcode = Gencode (5);

The context. session["Code"] = Checkcode;

System.Drawing.Bitmap image = New System.Drawing.Bitmap (70, 22);

Graphics g = graphics.fromimage (image);

Try

17 {

Random Random = new Random ();

19

G.clear (Color.White);

21st

int i;

for (i = 0; i < i++)

24 {

int x1 = Random. Next (image. Width);

num 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);

30}

31
  

Font font = new System.Drawing.Font ("Arial", (System.Drawing.FontStyle.Bold));

System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New R Ectangle (0, 0, image. Width, image. Height), Color.Blue, color.darkred, 1.2F, true);

g.DrawString (Checkcode, Font, brush, 2, 2);

35

G.drawrectangle (New Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1);

Panax Notoginseng System.IO.MemoryStream ms = new System.IO.MemoryStream ();

Image. Save (MS, System.Drawing.Imaging.ImageFormat.Png);

The context. Response.clearcontent ();

Context. Response.ContentType = "Image/png";

The context. Response.BinaryWrite (Ms. ToArray ());

42}

finally {

G.dispose ();

Image. Dispose ();

46}

47}

48

49//Generate random string

private string gencode (int num) {

Wuyi string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char[] Chastr = str. ToCharArray ();

String code = "";

Random rd = new Random ();

int i;

for (i = 0; i < num;i++)

57 {

The code = = str. Substring (Rd. Next (0, str. Length), 1);

59}

return code;

61}

The public bool IsReusable {

A/get {

return false;

65}

66}

67

}asp. How to use verification code in net

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.