简单验证码的产生

None.gif简单验证码的产生 
None.gifFrom :
None.gifhttp:
//singlepine.cnblogs.com/articles/264894.html (测试通过)
None.gif

None.gif
1.建立ValidateCode.aspx页面cs代码
None.gif
public class ValidateCode : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//如果要在页面a.aspx生成验证码,则在该页面添加一个图片控件,假设命名为:Image1,然后在page_Load事件中写如下代码:
InBlock.gif            
//ImageButton1.ImageUrl = "ValidateCode.aspx";
InBlock.gif            
//这样就可以生成验证码了,ValidateCode.aspx页面可以随便放在哪里,不过要注意Image1.src 要写对,同级可以直接写ValidateCode.aspx,上一级写../ValidateCode.aspx,很方便吧。
InBlock.gif

InBlock.gif            
if(!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//RndNum是一个自定义函数
InBlock.gif                
//这里的数字4代表显示的是4位的验证字符串!
InBlock.gif                
//string VNum=RndNum(4); 
InBlock.gif
                string VNum=GenerateRandom(4);
InBlock.gif                Session[
"VNum"= VNum;
InBlock.gif                Validate_Code(VNum);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Web Form Designer generated codeWeb Form Designer generated code
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//**//**//**//**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Validate_Code(string VNum) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int Gheight=(int)(VNum.Length * 11.5);
InBlock.gif            
//gheight为图片宽度,根据字符长度自动更改图片宽度
InBlock.gif
            System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20);
InBlock.gif            Graphics g 
= Graphics.FromImage(Img);
InBlock.gif            g.DrawString(VNum,
new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3); 
InBlock.gif            
//在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y) 
InBlock.gif
            System.IO.MemoryStream ms=new System.IO.MemoryStream();
InBlock.gif            Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png); 
InBlock.gif            Response.ClearContent(); 
//需要输出图象信息 要修改HTTP头 
InBlock.gif
            Response.ContentType="image/Png";
InBlock.gif            Response.BinaryWrite(ms.ToArray());
InBlock.gif            g.Dispose();
InBlock.gif            Img.Dispose(); 
InBlock.gif            Response.End();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private static char[] constant=
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
'0','1','2','3','4','5','6','7','8','9',
InBlock.gif            
'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',
InBlock.gif            
'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'
ExpandedSubBlockEnd.gif        }
;
InBlock.gif        
public static string GenerateRandom(int Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{   
InBlock.gif            System.Text.StringBuilder newRandom 
= new System.Text.StringBuilder(62);
InBlock.gif            Random rd
= new Random();
InBlock.gif            
for(int i=0;i<Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                newRandom.Append(constant[rd.Next(
62)]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return newRandom.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
2.建立演示页面Login.aspx,html代码
None.gif
<HTML>
None.gif    
<HEAD>
None.gif        
<title>Login</title>
None.gif        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
None.gif        
<meta name="CODE_LANGUAGE" Content="C#">
None.gif        
<meta name="vs_defaultClientScript" content="JavaScript">
None.gif        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
None.gif    
</HEAD>
None.gif    
<body>
None.gif        
<form id="Form1" method="post" runat="server">
None.gif            
<table align="center" cellSpacing="0" cellPadding="0" width="100%" border="0" height="100%">
None.gif                
<colgroup>
None.gif                    
<col width="30%">
None.gif                    
</col>
None.gif                    
<col width="40%">
None.gif                    
</col>
None.gif                    
<col width="30%">
None.gif                    
</col>
None.gif                
</colgroup>
None.gif                
<tr>
None.gif                    
<td></td>
None.gif                    
<td valign="middle">
None.gif                        
<TABLE id="Table1" align="center" cellSpacing="0" cellPadding="0" width="100%" border="0">
None.gif                            
<colgroup>
None.gif                                
<col width="20%">
None.gif                                
</col>
None.gif                                
<col width="35%">
None.gif                                
</col>
None.gif                                
<col width="25%">
None.gif                                
</col>
None.gif                                
<col width="20%">
None.gif                                
</col>
None.gif                            
</colgroup>
None.gif                            
<TR>
None.gif                                
<TD align="right">LoginName</TD>
None.gif                                
<TD>
None.gif                                    
<asp:TextBox id="txtLoginName" runat="server" Width="100%"></asp:TextBox></TD>
None.gif                                
<td></td>
None.gif                                
<td></td>
None.gif                            
</TR>
None.gif                            
<TR>
None.gif                                
<TD align="right">Password</TD>
None.gif                                
<TD>
None.gif                                    
<asp:TextBox id="txtPassword" runat="server" Width="100%"></asp:TextBox></TD>
None.gif                                
<td></td>
None.gif                                
<td></td>
None.gif                            
</TR>
None.gif                            
<TR>
None.gif                                
<TD align="right">ValidateCode</TD>
None.gif                                
<TD>
None.gif                                    
<asp:TextBox id="txtValidateCode" runat="server" Width="100%"></asp:TextBox></TD>
None.gif                                
<td><IMG alt="" src="Pages/Wonderful/Form/ValidateCode.aspx"></td>
None.gif                                
<td></td>
None.gif                            
</TR>
None.gif                            
<TR>
None.gif                                
<TD></TD>
None.gif                                
<TD>
None.gif                                    
<asp:Button id="Login" runat="server" Text="Login"></asp:Button>
None.gif                                    
<asp:Button id="Reset" runat="server" Text="Reset"></asp:Button></TD>
None.gif                                
<td></td>
None.gif                                
<td></td>
None.gif                            
</TR>
None.gif                        
</TABLE>
None.gif                    
</td>
None.gif                    
<td></td>
None.gif                
</tr>
None.gif            
</table>
None.gif        
</form>
None.gif    
</body>
None.gif
</HTML>
None.gif
3.Login页面的cs代码
None.gif
public class Login : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected System.Web.UI.WebControls.TextBox txtLoginName;
InBlock.gif    
protected System.Web.UI.WebControls.TextBox txtPassword;
InBlock.gif    
protected System.Web.UI.WebControls.TextBox txtValidateCode;
InBlock.gif    
protected System.Web.UI.WebControls.Button Login;
InBlock.gif    
protected System.Web.UI.WebControls.Button Reset;
InBlock.gif    
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(Session["VNum"]!=null && Session["VNum"].ToString()==txtValidateCode.Text)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//验证码正确
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Web Form Designer generated codeWeb Form Designer generated code
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//**//**//**//**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/aaliujing/archive/2006/12/18/595975.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值