asp.net Web controls that implement the authentication Code feature
Source: Internet
Author: User
Asp.net|web| Control | Verification code asp.net design and design concepts and other such as Asp,jsp,php,perl
And so are not the same, almost completely object-oriented design! The reuse of code is its
One of the major differences, asp.net in addition to the use of include, also provides
A more characteristic web control, including: Ascx form and control with design-time support
Pieces [This article belongs to the latter], in order to familiarize yourself with these new concepts, I wrote a Web control myself.
In the actual project to run well, later, to have time, I will continue to improve.
How to use the Validatecode control:
First step:
Compile the original code I provided, and then, on the Studio.NET 2003 toolbar, select Add/Remove items to select the compiled DLL file.
Step Two:
The toolbar will be one more Web control Validatecode, do a Web form, on the Studio.NET 2003 development interface, directly drag the control onto the WebForm, ok!
Step Three:
In this control's Graphicok event obtains, the authentication code The character information, uses in compares with the user entry!
Last step:
In the root directory of the Web site, build a temp directory (or you can specify the directory yourself) to store the authentication code
Pictures, don't worry, the code will automatically delete useless pictures!
The original code is as follows:
Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;
Using System.IO;
Using System.Drawing;
Using System.Drawing.Drawing2D;
Using System.Drawing.Imaging;
Namespace Webvalidatecode
{
<summary>
Summary description of the Validatecode.
Designer: Wang Haibo 2004-11-20
</summary>
///
public enum Graphictype
{
JPG = 0,
Gif = 1,
Png = 2,
BMP = 3,
}
[ToolboxBitmap (@ "D:\DotnetApp\ValidateCode\ValidateCode.bmp")]//Set the icon for the control on the Toolbox
public class ValidateCode:System.Web.UI.WebControls.WebControl, INamingContainer
{
private int pcodelen=5;
private int pchartwidth=100;
private int pchartheight=20;
Build the length of the checksum code
[Bindable (True), browsable (True), Category ("appearance"), DefaultValue (True), Description ("need to verify the length of the code, recommended between the 5~8 bit!")]
public int Codelength
{
Get
{
return Pcodelen;
}
Set
{
Pcodelen = value;
}
}
Build the length of the checksum code
[Bindable (True), browsable (True), Category ("appearance"), DefaultValue (True), Description ("Generate a temporary store path for a captcha picture," Requirement must be a virtual directory under the site! ")]
public string Tempimageurlpath
{
Get
{
return ptempimageurlpath;
}
Set
{
Ptempimageurlpath = value;
}
}
[Bindable (True), browsable (True), Category ("appearance"), DefaultValue (graphictype.jpg), Description ("Select type to generate a checksum diagram file" (JPG; Gif; Png; BMP)! ")]
Public Graphictype ChartType
{
Get
{
return pcharttype;
}
Set
{
Pcharttype = value;
}
}
Generate the width of the checksum code picture
public int Chartwidth
{
Get
{
return pchartwidth;
}
Set
{
Pchartwidth = value;
}
}
Build the height of the checksum code picture
public int Chartheight
{
Get
{
return pchartheight;
}
Set
{
Pchartheight = value;
}
}
The checksum code that needs to be generated
public string Authencode
{
Get
{
return pauthencode;
}
Set
{
Pauthencode = value;
}
}
#endregion
<summary>
Renders this control to the specified output parameter.
</summary>
<param name= "Output" > HTML writer to write </param>
protected override void Render (HtmlTextWriter output)
{
<summary>
Initialization of a property when the control is load
</summary>
<param name= "E" ></param>
protected override void OnLoad (System.EventArgs e)
{
Eraseoldgraphic (); Delete an expired picture
}
private void Getrandomimage (string strvalidatecode)
{
Generates the full name of the immediate picture, and the full URL
Makerandomfilename ();
Validateimage = new Bitmap (Pchartwidth, Pchartheight, Pixelformat.format24bpprgb); // . FORMAT24BPPRGB);
g = Graphics.fromimage (validateimage);
G.clear (Color.lightgray);
g.DrawString (Strvalidatecode, New Font ("Song Body", 16,fontstyle.bold), New SolidBrush (color.darkred), New PointF (2,2));
for (int i=0;i<strvalidatecode.length;i++)
{
Random r = new Random ();
PointF startpos=new PointF (R.next (3,6) + (R.next (12,14) *i), R.next ( -1,2));
g.DrawString (Strvalidatecode.substring (i,1), New Font ("XXFarEastFont-Arial", 14,fontstyle.italic), New SolidBrush (Color.Blue), STARTPOS);
}
G.fillrectangle (New LinearGradientBrush (0,0), new Point (120,30), Color.FromArgb (0,0,0,0), Color.FromArgb (255,255,255,255)), 0,0,120,30);
Switch (pcharttype)
{
Case Graphictype.jpg:
<summary>
Dynamic selection of generating parity codes from elements of numbers and letters
</summary>
private String Getvalidatecode ()
{
Char[] s = new char[]{' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' a '
, ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q '
, ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '
, ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W '
, ' X ', ' Y ', ' Z '};
String num = "";
Random r = new Random ();
Defines the number of digits of a verification code based on the length required by the user
for (int i = 0; i < codelength; i++)
{
num + + s[r.next (0, S.length)]. ToString ();
}
return num;
}
<summary>
Clears temporary picture records for more than 20 seconds
</summary>
<returns> returns true successfully, Failure returns false</returns>
public bool Eraseoldgraphic ()
{
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