简单验证码的产生

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

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

        }


        
Web Form Designer generated code

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

        
        
private static char[] constant=
        
{
            
'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'
        }
;
        
public static string GenerateRandom(int Length)
        
{   
            System.Text.StringBuilder newRandom 
= new System.Text.StringBuilder(62);
            Random rd
= new Random();
            
for(int i=0;i<Length;i++)
            
{
                newRandom.Append(constant[rd.Next(
62)]);
            }

            
return newRandom.ToString();
        }


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

        }


        
Web Form Designer generated code

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值