添加登陆界面,增加验证码功能。

------------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"

οnclick="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"

οnclick="this.src='HandlerPic.ashx?yzm='+new Date()" />
                </td>
            </tr>
            <tr>
                <td>  </td>
                <td> 
                    <asp:Button ID="btnLogin" runat="server" Text="登陆"

οnclick="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;
            }
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值