This article mainly introduces the method of using ASHX to generate the graphics verification code, analyzes the steps, implementation methods and related precautions of ASP.
An example of this article describes how ASP. NET uses ASHX to generate a graphical verification code. Share to everyone for your reference, as follows:
The benefits of captcha don't need me to say it, you all know it. I saw on the internet that someone wrote the verification code directly on the ASPX page, which means that it's not scientific to ask for a verification code that is equal to requesting a page. As shown below
<form id= "Form1" runat= "Server" > <p> <asp:image id= "Image1" runat= "Server" Imageurl= " Default.aspx "/> <br/> <asp:textbox id=" TextBox1 "runat=" Server "></asp:TextBox> <asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "button"/> </p></form>
This code looks like the person who writes the code, the code written in such a very depressed. Verify that you don't write a script to do the next switch.
Let me introduce a way to implement such a function
1. Write a ashx to generate a graphical verification code
Using system;using system.collections;using system.data;using system.linq;using system.web;using System.Web.Services ; using system.web.services.protocols;using system.xml.linq;using system.web.sessionstate;using System.Drawing; namespace usechecknum.ashx{//<summary>///$codebehindclassname $ for a summary description///</summary> [WebService (Names Pace = "http://tempuri.org/")] [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)] public class Docreatenum: ihttphandler,irequiressessionstate {public void ProcessRequest (HttpContext context) {context. Response.ContentType = "text/html"; String checkcode = Getvalidation (5); Generates a 5-bit random verification code character context. session["Code" = Checkcode; Save the string to the session so that it is validated when needed System.Drawing.Bitmap image = new System.Drawing.Bitmap (70, 22); Graphics g = graphics.fromimage (image); try {//Generate random generator randomly random = new random (); Empty the picture background color g.clear (color.white); Draw a picture of the background noise line int i; for (i = 0; i <; 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", and (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); Picture the foreground noise point G.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 {g.dispose (); Image. Dispose (); }} public string getvalidation (int num) {string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//"or writing Chinese characters is OK" String validatecode = ""; Random rd = new random (); for (int i = 0; i < num; i++) {validatecode + = str. Substring (Rd. Next (0, str. Length), 1); } return Validatecode; } public bool IsReusable {get {return false; } } }}
2. Display the Verification code on the page, because we generate a graphic, so can be directly written in the tag, we just write a simple script can click the mouse to switch the verification code
<%@ page language= "C #" autoeventwireup= "true" codebehind= "Index.aspx.cs" inherits= "Usechecknum._default"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
For a while, it's time to see what the generated captcha looks like.