asp.net verification code generation and refresh and verification _ practical skills

Source: Internet
Author: User
Verification code technology is designed to prevent violent cracking, and so on. Now the general website registration and so on provide authentication code function, especially Tencent is a long string. The text refers to someone else's code. There is no need to write again. can read it. But I found two pageload problems during the test. Two sentences can be commented on. The namespaces was modified at the same time. Complete validation instructions are also available:
1 new verifycode.aspx
The CS file code is as follows:
Copy Code code as follows:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Drawing.Text;
/**/////<summary>
Page Verification Code Program
Using: Add HTML code to the page
</summary>
public partial class VerifyCode:System.Web.UI.Page
... {
Static string[] Fontitems = new string[] ... {"Arial",
"Helvetica",
"Geneva",
"Sans-serif",
"Verdana"
};
Static brush[] Brushitems = new brush[] ... {Brushes.olivedrab,
Brushes.forestgreen,
Brushes.darkcyan,
Brushes.lightslategray,
Brushes.royalblue,
Brushes.slateblue,
Brushes.darkviolet,
Brushes.mediumvioletred,
Brushes.indianred,
Brushes.firebrick,
Brushes.chocolate,
Brushes.peru,
Brushes.goldenrod
};
Static string[] Brushname = new string[] ... {"Olivedrab",
"Forestgreen",
"Darkcyan",
"Lightslategray",
"Royalblue",
"Slateblue",
"Darkviolet",
"Mediumvioletred",
"Indianred",
"Firebrick",
"Chocolate",
"Peru",
"Goldenrod"
};
private static Color BackColor = Color.White;
private static Pen bordercolor = Pens.darkgray;
private static int Width = 52;
private static int Height = 21;
Private Random _random;
private string _code;
private int _brushnameindex;
Override protected void OnInit (EventArgs e)
... {
//
Codegen:this the call are required by the ASP.net Web Form Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
/**//**//**////<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
... {
This. Load + = new System.EventHandler (this. Page_Load);
}
/**////<summary>
///
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
public void Page_Load (object sender, System.EventArgs e)
... {
if (! IsPostBack)
... {
//
Todo:initialize
//
This._random = new Random ();
This._code = Getrandomcode ();
//
Todo:use session["Code"] Save the Verifycode
//
session["code"] = This._code;
//
Todo:output Image
//
This. Setpagenocache ();
This. OnPaint ();
}
}
/**//**//**////<summary>
Set the page not to be cached
</summary>
private void Setpagenocache ()
... {
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds (-1);
Response.Expires = 0;
Response.CacheControl = "No-cache";
Response.appendheader ("Pragma", "No-cache");
}
/**//**//**////<summary>
To get a 4-bit random code
</summary>
<returns></returns>
private String Getrandomcode ()
... {
Return Guid.NewGuid (). ToString (). Substring (0, 4);
}
/**//**//**////<summary>
Randomly take a font
</summary>
<returns></returns>
Private Font GetFont ()
... {
int fontindex = _random. Next (0, fontitems.length);
FontStyle FontStyle = Getfontstyle (_random. Next (0, 2));
return new Font (Fontitems[fontindex], fontstyle);
}
/**//**//**////<summary>
Take the style of a font
</summary>
<param name= "Index" ></param>
<returns></returns>
Private FontStyle getfontstyle (int index)
... {
Switch (index)
... {
Case 0:
return fontstyle.bold;
Case 1:
return fontstyle.italic;
Default
return fontstyle.regular;
}
}
/**//**//**////<summary>
Randomly take a brush
</summary>
<returns></returns>
Private Brush Getbrush ()
... {
int brushindex = _random. Next (0, brushitems.length);
_brushnameindex = Brushindex;
return Brushitems[brushindex];
}
/**//**//**////<summary>
Painting events
</summary>
private void OnPaint ()
... {
Bitmap objbitmap = null;
Graphics g = null;
Try
... {
Objbitmap = new Bitmap (Width, Height);
g = Graphics.fromimage (Objbitmap);
Paint_background (g);
Paint_text (g);
Paint_textstain (OBJBITMAP);
Paint_border (g);
Objbitmap.save (Response.outputstream, imageformat.gif);
Response.ContentType = "Image/gif";
}
Catch... {}
Finally
... {
if (null!= objbitmap)
Objbitmap.dispose ();
if (null!= g)
G.dispose ();
}
}
/**//**//**////<summary>
Painting background color
</summary>
<param name= "G" ></param>
private void Paint_background (Graphics g)
... {
G.clear (BackColor);
}
/**//**//**////<summary>
Painting borders
</summary>
<param name= "G" ></param>
private void Paint_border (Graphics g)
... {
G.drawrectangle (bordercolor, 0, 0, Width-1, Height-1);
}
/**//**//**////<summary>
Painting text
</summary>
<param name= "G" ></param>
private void Paint_text (Graphics g)
... {
g.DrawString (_code, GetFont (), Getbrush (), 3, 1);
}
/**//**//**////<summary>
Drawing text Noise Point
</summary>
<param name= "G" ></param>
private void Paint_textstain (Bitmap b)
... {
for (int n=0; n<30; n++)
... {
int x = _random. Next (Width);
int y = _random. Next (Height);
B.setpixel (x, Y, Color.fromname (brushname[_brushnameindex));
}
}
}

2 page reference:
<asp:image id= "GetCode" src= "verifycode.aspx" runat= "Server"/>
Generally need to provide refresh at the same time (see not clear for another), the code is as follows
<asp:image id= "GetCode" src= "verifycode.aspx" runat= "Server"/> <a "href= ()" > Refresh Verification Code </A>
If you use a master page, use the following code:
<%--<asp:image id=" GetCode "src=" VerifyCode.aspx "runat= "Server"/>--%>
<a href= "Javascript:getimgcode ()" > Refresh Verification Code </A>
Hyperlinks correspond to JavaScript as follows:
<script language= "javascript" type= "Text/javascript" >
function Getimgcode ()
{
var getimagecode = document.getElementById ("GetCode");
GETIMAGECODE.SRC = "verifycode.aspx";
}
</script>
3 Judgment Verification
The above code is to store the verification code in the session, using code to mark. Read the code session["Codes". ToString ();
In use, we only need to compare session["code". ToString () and the text box input string (textboxcode.text) is the same can be judged.
if (session["code"]. ToString (). Trim (). Equals (TextBoxCode.Text.Trim ()))
... {
Response.Write ("Success");
}
Test pass!
Related Article

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.