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

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

 Web Form Designer generated codeWeb Form Designer generated code
        Web Form Designer generated codeWeb Form Designer generated code

 private void Validate_Code(string VNum)
        private void Validate_Code(string VNum) 
 {
        {
 int Gheight=(int)(VNum.Length * 11.5);
            int Gheight=(int)(VNum.Length * 11.5);
 //gheight为图片宽度,根据字符长度自动更改图片宽度
            //gheight为图片宽度,根据字符长度自动更改图片宽度
 System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20);
            System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20);
 Graphics g = Graphics.FromImage(Img);
            Graphics g = Graphics.FromImage(Img);
 g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3);
            g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3); 
 //在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
            //在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y) 
 System.IO.MemoryStream ms=new System.IO.MemoryStream();
            System.IO.MemoryStream ms=new System.IO.MemoryStream();
 Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
            Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png); 
 Response.ClearContent(); //需要输出图象信息 要修改HTTP头
            Response.ClearContent(); //需要输出图象信息 要修改HTTP头 
 Response.ContentType="image/Png";
            Response.ContentType="image/Png";
 Response.BinaryWrite(ms.ToArray());
            Response.BinaryWrite(ms.ToArray());
 g.Dispose();
            g.Dispose();
 Img.Dispose();
            Img.Dispose(); 
 Response.End();
            Response.End();
 }
        }
 
        
 private static char[] constant=
        private static char[] constant=
 {
        {
 '0','1','2','3','4','5','6','7','8','9',
            '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',
 '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)
        public static string GenerateRandom(int Length)
 {
        {   
 System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
 Random rd= new Random();
            Random rd= new Random();
 for(int i=0;i<Length;i++)
            for(int i=0;i<Length;i++)
 {
            {
 newRandom.Append(constant[rd.Next(62)]);
                newRandom.Append(constant[rd.Next(62)]);
 }
            }
 return newRandom.ToString();
            return newRandom.ToString();
 }
        }

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

 Web Form Designer generated codeWeb Form Designer generated code
        Web Form Designer generated codeWeb Form Designer generated code

 }
}
http://singlepine.cnblogs.com/articles/264894.html (测试通过)
1.建立ValidateCode.aspx页面cs代码
 public class ValidateCode : System.Web.UI.Page
public class ValidateCode : System.Web.UI.Page {
    { private void Page_Load(object sender, System.EventArgs e)
        private void Page_Load(object sender, System.EventArgs e) {
        { //如果要在页面a.aspx生成验证码,则在该页面添加一个图片控件,假设命名为:Image1,然后在page_Load事件中写如下代码:
            //如果要在页面a.aspx生成验证码,则在该页面添加一个图片控件,假设命名为:Image1,然后在page_Load事件中写如下代码: //ImageButton1.ImageUrl = "ValidateCode.aspx";
            //ImageButton1.ImageUrl = "ValidateCode.aspx"; //这样就可以生成验证码了,ValidateCode.aspx页面可以随便放在哪里,不过要注意Image1.src 要写对,同级可以直接写ValidateCode.aspx,上一级写../ValidateCode.aspx,很方便吧。
            //这样就可以生成验证码了,ValidateCode.aspx页面可以随便放在哪里,不过要注意Image1.src 要写对,同级可以直接写ValidateCode.aspx,上一级写../ValidateCode.aspx,很方便吧。
 if(!IsPostBack)
            if(!IsPostBack) {
            { //RndNum是一个自定义函数
                //RndNum是一个自定义函数 //这里的数字4代表显示的是4位的验证字符串!
                //这里的数字4代表显示的是4位的验证字符串! //string VNum=RndNum(4);
                //string VNum=RndNum(4);  string VNum=GenerateRandom(4);
                string VNum=GenerateRandom(4); Session["VNum"] = VNum;
                Session["VNum"] = VNum; Validate_Code(VNum);
                Validate_Code(VNum); }
            } }
        }
 Web Form Designer generated codeWeb Form Designer generated code
        Web Form Designer generated codeWeb Form Designer generated code
 private void Validate_Code(string VNum)
        private void Validate_Code(string VNum)  {
        { int Gheight=(int)(VNum.Length * 11.5);
            int Gheight=(int)(VNum.Length * 11.5); //gheight为图片宽度,根据字符长度自动更改图片宽度
            //gheight为图片宽度,根据字符长度自动更改图片宽度 System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20);
            System.Drawing.Bitmap Img = new System.Drawing.Bitmap(Gheight,20); Graphics g = Graphics.FromImage(Img);
            Graphics g = Graphics.FromImage(Img); g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3);
            g.DrawString(VNum,new System.Drawing.Font("Arial",10),new System.Drawing.SolidBrush(Color.Red),3,3);  //在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)
            //在矩形内绘制字串(字串,字体,画笔颜色,左上x.左上y)  System.IO.MemoryStream ms=new System.IO.MemoryStream();
            System.IO.MemoryStream ms=new System.IO.MemoryStream(); Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
            Img.Save(ms,System.Drawing.Imaging.ImageFormat.Png);  Response.ClearContent(); //需要输出图象信息 要修改HTTP头
            Response.ClearContent(); //需要输出图象信息 要修改HTTP头  Response.ContentType="image/Png";
            Response.ContentType="image/Png"; Response.BinaryWrite(ms.ToArray());
            Response.BinaryWrite(ms.ToArray()); g.Dispose();
            g.Dispose(); Img.Dispose();
            Img.Dispose();  Response.End();
            Response.End(); }
        } 
         private static char[] constant=
        private static char[] constant= {
        { '0','1','2','3','4','5','6','7','8','9',
            '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', '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)
        public static string GenerateRandom(int Length) {
        {    System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62); Random rd= new Random();
            Random rd= new Random(); for(int i=0;i<Length;i++)
            for(int i=0;i<Length;i++) {
            { newRandom.Append(constant[rd.Next(62)]);
                newRandom.Append(constant[rd.Next(62)]); }
            } return newRandom.ToString();
            return newRandom.ToString(); }
        }
 }
    }2.建立演示页面Login.aspx,html代码
 <HTML>
<HTML> <HEAD>
    <HEAD> <title>Login</title>
        <title>Login</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD>
    </HEAD> <body>
    <body> <form id="Form1" method="post" runat="server">
        <form id="Form1" method="post" runat="server"> <table align="center" cellSpacing="0" cellPadding="0" width="100%" border="0" height="100%">
            <table align="center" cellSpacing="0" cellPadding="0" width="100%" border="0" height="100%"> <colgroup>
                <colgroup> <col width="30%">
                    <col width="30%"> </col>
                    </col> <col width="40%">
                    <col width="40%"> </col>
                    </col> <col width="30%">
                    <col width="30%"> </col>
                    </col> </colgroup>
                </colgroup> <tr>
                <tr> <td></td>
                    <td></td> <td valign="middle">
                    <td valign="middle"> <TABLE id="Table1" align="center" cellSpacing="0" cellPadding="0" width="100%" border="0">
                        <TABLE id="Table1" align="center" cellSpacing="0" cellPadding="0" width="100%" border="0"> <colgroup>
                            <colgroup> <col width="20%">
                                <col width="20%"> </col>
                                </col> <col width="35%">
                                <col width="35%"> </col>
                                </col> <col width="25%">
                                <col width="25%"> </col>
                                </col> <col width="20%">
                                <col width="20%"> </col>
                                </col> </colgroup>
                            </colgroup> <TR>
                            <TR> <TD align="right">LoginName</TD>
                                <TD align="right">LoginName</TD> <TD>
                                <TD> <asp:TextBox id="txtLoginName" runat="server" Width="100%"></asp:TextBox></TD>
                                    <asp:TextBox id="txtLoginName" runat="server" Width="100%"></asp:TextBox></TD> <td></td>
                                <td></td> <td></td>
                                <td></td> </TR>
                            </TR> <TR>
                            <TR> <TD align="right">Password</TD>
                                <TD align="right">Password</TD> <TD>
                                <TD> <asp:TextBox id="txtPassword" runat="server" Width="100%"></asp:TextBox></TD>
                                    <asp:TextBox id="txtPassword" runat="server" Width="100%"></asp:TextBox></TD> <td></td>
                                <td></td> <td></td>
                                <td></td> </TR>
                            </TR> <TR>
                            <TR> <TD align="right">ValidateCode</TD>
                                <TD align="right">ValidateCode</TD> <TD>
                                <TD> <asp:TextBox id="txtValidateCode" runat="server" Width="100%"></asp:TextBox></TD>
                                    <asp:TextBox id="txtValidateCode" runat="server" Width="100%"></asp:TextBox></TD> <td><IMG alt="" src="Pages/Wonderful/Form/ValidateCode.aspx"></td>
                                <td><IMG alt="" src="Pages/Wonderful/Form/ValidateCode.aspx"></td> <td></td>
                                <td></td> </TR>
                            </TR> <TR>
                            <TR> <TD></TD>
                                <TD></TD> <TD>
                                <TD> <asp:Button id="Login" runat="server" Text="Login"></asp:Button>
                                    <asp:Button id="Login" runat="server" Text="Login"></asp:Button> <asp:Button id="Reset" runat="server" Text="Reset"></asp:Button></TD>
                                    <asp:Button id="Reset" runat="server" Text="Reset"></asp:Button></TD> <td></td>
                                <td></td> <td></td>
                                <td></td> </TR>
                            </TR> </TABLE>
                        </TABLE> </td>
                    </td> <td></td>
                    <td></td> </tr>
                </tr> </table>
            </table> </form>
        </form> </body>
    </body> </HTML>
</HTML>3.Login页面的cs代码
 public class Login : System.Web.UI.Page
public class Login : System.Web.UI.Page {
{ protected System.Web.UI.WebControls.TextBox txtLoginName;
    protected System.Web.UI.WebControls.TextBox txtLoginName; protected System.Web.UI.WebControls.TextBox txtPassword;
    protected System.Web.UI.WebControls.TextBox txtPassword; protected System.Web.UI.WebControls.TextBox txtValidateCode;
    protected System.Web.UI.WebControls.TextBox txtValidateCode; protected System.Web.UI.WebControls.Button Login;
    protected System.Web.UI.WebControls.Button Login; protected System.Web.UI.WebControls.Button Reset;
    protected System.Web.UI.WebControls.Button Reset; protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.TextBox TextBox1; private void Page_Load(object sender, System.EventArgs e)
private void Page_Load(object sender, System.EventArgs e) {
        { if(Session["VNum"]!=null && Session["VNum"].ToString()==txtValidateCode.Text)
            if(Session["VNum"]!=null && Session["VNum"].ToString()==txtValidateCode.Text) {
            { //验证码正确
                //验证码正确 }
            } }
        }
 Web Form Designer generated codeWeb Form Designer generated code
        Web Form Designer generated codeWeb Form Designer generated code
 }
} 
                    
                 
    
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号