1. handler file Validateimagehandler.ashx code as follows
<% @ WebHandler Language = "C #" Class = "Validateimagehandler"%>
Using System;
Using System.Web;
Using System.Web.SessionState;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Text;
/**////<summary>
Validateimagehandler Build site authentication code feature
</summary>
public class Validateimagehandler:ihttphandler, IRequiresSessionState
{
int intlength = 5; Length
String stridentify = "identify"; A random string stores key values so that they are stored in session
Public Validateimagehandler ()
{
}
/**////<summary>
Generate validation Picture Core code
</summary>
<param name= "HC" ></param>
public void ProcessRequest (HttpContext hc)
{
Format output stream picture
Hc. Response.ContentType = "Image/gif";
Bitmap B = new Bitmap (200, 60);
Graphics g = graphics.fromimage (b);
G.fillrectangle (New SolidBrush (Color.yellowgreen), 0, 0, 200, 60);
Font font = new Font (FONTFAMILY.GENERICSERIF, fontstyle.bold, GraphicsUnit.Pixel);
Random r = new Random ();
List of legitimate randomly displayed characters
String strletters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder s = new StringBuilder ();
Draw a randomly generated string onto a picture
for (int i = 0; i < intlength; i++)
{
S.append (strletters.substring (r.next (0, strletters.length-1), 1));
g.DrawString (S[s.length-1]. ToString (), Font, New SolidBrush (Color.Blue), I *, R.next (0, 15));
}
Generate interference lines
Pen pen = new Pen (new SolidBrush (Color.Blue), 2);
for (int i = 0; i < i++)
{
G.drawline (pen, New Point (R.next (0, 199), R.next (0)), New Point (R.next (0, 199), R.next (0, 59));
}
B.save (HC. Response.outputstream, imageformat.gif);
Hc. Session[stridentify] = s.tostring (); First save in session, verify consistency with user input
Hc. Response.End ();
}
/**////<summary>
Indicates whether such instances can be shared by multiple requests (reuse can improve performance)
</summary>
public bool IsReusable
{
Get
{
return true;
}
}
}
2. Front Code
<% @ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head runat = "server" >
< title > Imagevalidatecode_httphandler </title >
< BODY >
< form ID = "Form1" runat = "server" >
< div >
< br/>
< br/>
< Asp:login ID = "Login1" runat = "server" BackColor = "#EFF3FB" bordercolor = "#B5C7DE" borderpadding = "4"
BorderStyle = "Solid" borderwidth = "1px" font-names = "Verdana" font-size = "0.8em"
ForeColor = "#333333" onauthenticate = "Login1_authenticate" >
< TitleTextStyle BackColor = "#507CD1" font-bold = "True" font-size = "0.9em" ForeColor = "white"/>
< InstructionTextStyle font-italic = "True" ForeColor = "Black"/>
< TextboxStyle font-size = "0.8em"/>
< Loginbuttonstyle BackColor = "white" bordercolor = "#507CD1" BorderStyle = "Solid" borderwidth = "1px"
Font-names = "Verdana" font-size = "0.8em" ForeColor = "#284E98"/>
< LayoutTemplate >
< table border