MyException - 我的异常网
当前位置:我的异常网» 移动开发 » 温故知新-增添登陆界面,增加验证码功能

温故知新-增添登陆界面,增加验证码功能

www.MyException.Cn  网友分享于:2013-02-21  浏览:2次
温故知新--添加登陆界面,增加验证码功能

-----------index.aspx--------------------
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width:40%; height: 124px;">
            <tr>
                <td>用户名:</td>
                <td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> </td>
            </tr>
            <tr>
                <td>密 码:</td>
                <td><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>数字验证码:</td>
                <td>
                    <asp:TextBox ID="txtYanZheng1" runat="server" Width="61px" Height="19px"></asp:TextBox><asp:Image ID="Image1" runat="server" ImageUrl="~/pic.aspx" onclick="this.src='pic.aspx?yzm='+new Date()"/></td>
            </tr>
            <tr>
                <td>  字母验证码:</td>
                <td> 
                    <asp:TextBox ID="txtYanZheng2" runat="server" Width="61px" Height="19px"></asp:TextBox>  <img alt="" src="HandlerPic.ashx" onclick="this.src='HandlerPic.ashx?yzm='+new Date()" />
                </td>
            </tr>
            <tr>
                <td>  </td>
                <td> 
                    <asp:Button ID="btnLogin" runat="server" Text="登陆" onclick="btnLogin_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>


------------index.aspx.cs--------------------------
  public partial class index : System.Web.UI.Page,IRequiresSessionState
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text != "admin")
            {
                Response.Write("用户名不正确!");
            }
            else if(txtPassword.Text != "111111")
            {
                Response.Write("密码不正确!");
            }
            else if (txtYanZheng1.Text!= Session["YZM"].ToString())
            {
                Response.Write("数字验证码不正确!");
            }
            else if (txtYanZheng2.Text != Session["CheckCode"].ToString())
            {
                Response.Write("字母验证码不正确!");
            }
            else
            {
                Response.Write("登陆成功!");
            }
        }
    }

--------------pic.aspx.cs--------------------------
 protected void Page_Load(object sender, EventArgs e)
        {
            System.Drawing.Image img = new Bitmap(150,60);
            Graphics g = Graphics.FromImage(img);
            g.FillRectangle(new SolidBrush(Color.SkyBlue), 0, 0, 200, 60);
            this.AddPoint(img, 200);
            string code = this.GeneralCode();
            Font font1 = new Font("宋体", 30, FontStyle.Italic);
            g.DrawString(code, font1, Brushes.Tomato,0,0);
            this.Response.Clear();
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            this.Response.BinaryWrite(ms.ToArray());
            this.Response.Flush();
            this.Response.End();
        }

        private void AddPoint(System.Drawing.Image img, int nums)
        {
            Bitmap b = img as Bitmap;
            Random ran = new Random();
            for (int i = 0; i < nums; i++)
            {
                b.SetPixel(ran.Next(0, img.Width), ran.Next(0, img.Height), Color.Tomato);
            }
        }

        private string GeneralCode()
        {
            Random ran = new Random(DateTime.Now.Millisecond);
            StringBuilder sb = new StringBuilder(6);
            for (int i = 0; i < 6; i++)
            {
                sb.Append(ran.Next(0, 9));
            }
            Session["YZM"] = sb.ToString();
            return sb.ToString();
        }

---------------HandlerPic.ashx--------------------------------
  public class HandlerPic : IHttpHandler, IRequiresSessionState //
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            //建立Bitmap对象,绘图
            Bitmap basemap = new Bitmap(150, 60);
            Graphics graph = Graphics.FromImage(basemap);
            graph.FillRectangle(new SolidBrush(Color.SkyBlue), 0, 0, 160, 60);
            Font font = new Font(FontFamily.GenericSerif, 30, FontStyle.Bold, GraphicsUnit.Pixel);
            Random r = new Random();
            string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
            string letter;
            StringBuilder sb = new StringBuilder();

            //添加随机的五个字母
            for (int x = 0; x < 5; x++)
            {
                letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
                sb.Append(letter);
                graph.DrawString(letter, font, new SolidBrush(Color.Tomato), x * 20, r.Next(0, 15));
            }

            //混淆背景
            Pen linePen = new Pen(new SolidBrush(Color.Tomato), 2);
            for (int x = 0; x < 6; x++)
                graph.DrawLine(linePen, new Point(r.Next(0, 170), r.Next(0,40)), new Point(r.Next(0, 170), r.Next(0, 40)));

            //将图片保存到输出流中     
            basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
            context.Session["CheckCode"] = sb.ToString();   //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有