.net学习笔记2--登陆页面及验证码设置

设计验证码的思路:在一个页面随机生成一串字符串,将字符串转成图片,然后在另一个页面引用。
主页面css:

  <style type="text/css">
    .background{
    text-align:center;
    position:relative;
    left:30%;
    width:545px;
    height:290px;
    background-image:url(images/gongqiu.GIF);
    }
   .box{
   position:relative;
   top:120px;
   }
    </style>

主页面JS:

 <script type="text/javascript">
       i = 0;
       function cl() {
           i++;
           document.getElementById('ig').src = "Default.aspx?i=" + i;
       }
    </script>

主页面body:

<body>
    <form id="form1" runat="server">
      <div class="background">
        <div class="box">
        <p>用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></p>
      <p>密码:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox></p>
      <p>请输入验证码:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
      </p>
      <img src="Default.aspx?i=0" id="ig" onclick="cl();" alt=""  />			      
          <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/denglu.gif" 
                onclick="ImageButton1_Click" />
          <asp:ImageButton ID="ImageButton2" runat="server"  ImageUrl="~/images/chongzhi.gif"/>
      </div>
      </div>
    </form>
</body>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if ( Response.Cookies["user"] != null)
        //{
        //    HttpCookie co = Response.Cookies["user"];
        //    TextBox1.Text = co["name"];
        //}
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (TextBox1.Text == "like" & TextBox2.Text == "123")
        {
            //HttpCookie co = new HttpCookie("user");
            //co["name"] = TextBox1.Text;
            //co.Expires = DateTime.Now.AddDays(1);
            //Response.Cookies.Add(co);

            string code = this.TextBox3.Text;
            //从cookie中取出验证码
            HttpCookie cookie = Request.Cookies["CheckCode"];
            if (code == cookie.Value.ToString())
            {
                Response.Write("<script>alert('登陆成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('登陆失败')</script>");
            }

            Response.Write("<script>document.location=document.location</script>");
        }
    }
}

验证码页面body:

  <form id="form1" runat="server">
    <div>
        <img src="Default.aspx"  alt=" " />
    </div>
    </form>

随机生成验证码后台代码:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        GetImage();


    }

    string GetRandom()
    {
        int i = 0;
        char c = ' ';
        ArrayList ar = new ArrayList();
        for (i = 0; i < 10; i++)
        {
            ar.Add(i);
        }
        for (i = 65; i < 91; i++)
        {
            c = (char)i;
            ar.Add(c);
        }
        for (i = 97; i < 123; i++)
        {
            c = (char)i;
            ar.Add(c);
        }
        int len = ar.Count;
        Random rd = new Random();
        string str = "";
        for (i = 0; i < 4; i++)
        {
            int n = rd.Next(62);
            str += ar[n].ToString();
        }
        Response.Cookies.Add(new HttpCookie("CheckCode", str));
        return str;
       
    }

    void GetImage()
    {
        string str = GetRandom();
        Session["check"] = str;
        MemoryStream stream = new MemoryStream();
        Bitmap img = new Bitmap(80, 30);
        Graphics g = Graphics.FromImage(img);
        g.Clear(Color.White);
        Font font = new Font("宋体", 20, FontStyle.Bold);
        Brush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Red, 1.2f, true); //new SolidBrush(Color.Red);
        g.DrawString(str, font, brush, 2, 2);
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(stream.ToArray());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值