ASP. NET -- create a verification code
We often encounter a verification code entry when logging on to the website, posting a blog or submitting a comment. At that time, we finally got in touch with niu Yi's news publishing system, here is a summary.
There are three things used: An aspx page and an image.
First, create An aspx page.
<% @ WebHandler Language = "C #" Class = "Picture" %> using System; using System. web; using System. drawing; using System. drawing. drawing2D; using System. web. sessionState; public class Picture: IHttpHandler, IRequiresSessionState // You must implement this interface to use session. Remember to import System. web. sessionState namespace {public void ProcessRequest (HttpContext context) {string checkCode = GenCode (5); // generates a five-character random context. session ["Code"] = checkCode; // Save the string to the Session so that System can be verified as needed. drawing. bitmap image = new System. drawing. bitmap (70, 22); Graphics g = Graphics. fromImage (image); try {// generate Random generator random Random = new Random (); // clear the image background color g. clear (Color. white); // specifies the background noise line of the image. int I; for (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, y1, x2, y2);} Font font = new System. drawing. font ("Arial", 12, (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); // The foreground noise of the picture. 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 ();}}////// Generate a random string //////Random output of several characters///
Random string
Private string GenCode (int num) {string str = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char [] chastr = str. toCharArray (); string code = ""; Random rd = new Random (); int I; for (I = 0; I <num; I ++) {// code + = source [rd. next (0, source. length)]; code + = str. substring (rd. next (0, str. length), 1) ;}return code ;}public bool IsReusable {get {return false ;}}}
Next, add an img control on the interface that uses the verification code, and the src property points to the. ashx file.
Then, determine whether the submitted verification code is correct.
// Determine whether the verification code is entered correctly string code = txtCode. text. trim (). toUpper (); // convert all input letters to uppercase and then compare them to string rightCode = Session ["Code"]. toString (); if (code! = RightCode) {Page. ClientScript. RegisterStartupScript (Page. GetType (), "message", "<script language = 'javascript 'defer> alert ('verification code input error! '); </Script> "); // The system prompts an error to ensure that the background remains unchanged. return ;}
Finally, use the commit crip to refresh the verification code. Because there are few codes, they can be nested in the code.
<Script language = "javascript" type = "text/javascript"> function changeCode () {var imgNode = document. getElementById ("vimg"); imgNode. src = "http://blog.csdn.net/zjx86320/article/details/handler/WaterMark.ashx? T = "+ (new Date (). valueOf (); // here, a time parameter is added to prevent browser cache problems.} </script>