登陆验证码简单制作

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代码

 

 1 <HTML>
 2     <HEAD>
 3         <title>Login</title>
 4         <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
 5         <meta name="CODE_LANGUAGE" Content="C#">
 6         <meta name="vs_defaultClientScript" content="JavaScript">
 7         <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 8     </HEAD>
 9     <body>
10         <form id="Form1" method="post" runat="server">
11             <table align="center" cellSpacing="0" cellPadding="0" width="100%" border="0" height="100%">
12                 <colgroup>
13                     <col width="30%">
14                     </col>
15                     <col width="40%">
16                     </col>
17                     <col width="30%">
18                     </col>
19                 </colgroup>
20                 <tr>
21                     <td></td>
22                     <td valign="middle">
23                         <TABLE id="Table1" align="center" cellSpacing="0" cellPadding="0" width="100%" border="0">
24                             <colgroup>
25                                 <col width="20%">
26                                 </col>
27                                 <col width="35%">
28                                 </col>
29                                 <col width="25%">
30                                 </col>
31                                 <col width="20%">
32                                 </col>
33                             </colgroup>
34                             <TR>
35                                 <TD align="right">LoginName</TD>
36                                 <TD>
37                                     <asp:TextBox id="txtLoginName" runat="server" Width="100%"></asp:TextBox></TD>
38                                 <td></td>
39                                 <td></td>
40                             </TR>
41                             <TR>
42                                 <TD align="right">Password</TD>
43                                 <TD>
44                                     <asp:TextBox id="txtPassword" runat="server" Width="100%"></asp:TextBox></TD>
45                                 <td></td>
46                                 <td></td>
47                             </TR>
48                             <TR>
49                                 <TD align="right">ValidateCode</TD>
50                                 <TD>
51                                     <asp:TextBox id="txtValidateCode" runat="server" Width="100%"></asp:TextBox></TD>
52                                 <td><IMG alt="" src="Pages/Wonderful/Form/ValidateCode.aspx"></td>
53                                 <td></td>
54                             </TR>
55                             <TR>
56                                 <TD></TD>
57                                 <TD>
58                                     <asp:Button id="Login" runat="server" Text="Login"></asp:Button>
59                                     <asp:Button id="Reset" runat="server" Text="Reset"></asp:Button></TD>
60                                 <td></td>
61                                 <td></td>
62                             </TR>
63                         </TABLE>
64                     </td>
65                     <td></td>
66                 </tr>
67             </table>
68         </form>
69     </body>
70 </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)
            
{
                
//验证码正确
            }

        }

}

 

posted @ 2012-10-16 14:37  meguoling  阅读(204)  评论(0编辑  收藏  举报