Session验证码的原理

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

namespace session验证码
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //我们平常在在登陆网页的时候需要输的“入验证码”的原理
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Random read = new Random(); //
                int i = read.Next(1000,10000);  //产生一个四位的随机数(验证码)
                Session["验证码"] = i; //将这个随机数保存到session中去

                TextBox1.Text = Convert.ToString(Session["验证码"]);//将这个随机数用TextBox1控件显示出来(当然,这里只是讲随机数原理性的东西。实际的验证码是给他放到一副图片里去的)

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string yzm = Convert.ToString(Session["验证码"]); //取得session中的随机数(验证码)
            if (TextBox2.Text == yzm) //如果随机数与TextBox2中输入的数据一致。那么就正确,否则错误
            {
                Label1.Text = "正确";
            }
            else
            {
                Label1.Text = "错误";
            }
        }
    }
}



****************************************************************************写一个验证码

WebForm1.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.11.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#TextBox1").focus(function () {
                if ($(this).val("请输入验证码")) {
                    $(this).val("");
                    $("#Label2").text("");
                }
            })
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%--这个Label1用来显示验证码--%>
        <label>验证码:</label><asp:Label ID="Label1" runat="server" Text="Label" style=" background:red; border:1px solid yellow"></asp:Label>

        <%--这个button2用来更换新的验证码--%>
        <asp:Button ID="Button2" runat="server" Text="看不清" οnclick="Button2_Click" /><br/>
        
        <%--这个TextBox用来让用户输入验证码--%>
        <asp:TextBox ID="TextBox1" runat="server" value="请输入验证码"></asp:TextBox>

        <%--提交验证码--%>
        <asp:Button ID="Button1" runat="server" Text="提交" οnclick="Button1_Click" 
            style="height: 21px" />

        <%--这个Label用来提示验证码是否错误--%>
        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>


WebForm1.cs

<pre class="csharp" name="code">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Session验证码
{
    public partial class WebForm1 : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //WebForm1 web = new WebForm1(); //不需要WebForm1 web = new WebForm1() 因为new WebForm1等于又从新产生了一个对象。
                yzm();
            }
            
        }

        
        //提交验证码
        protected void Button1_Click(object sender, EventArgs e)
        {
            string yzm = Session["value"].ToString(); //获取当前Session的值(即验证码的值)
            if (TextBox1.Text == yzm)
            {
                Label2.Text = "正确";
            }
            else
            {
                Label2.Text = "错误";
            }
        }

        //跟换新的验证码(当用户看不清楚验证码时候,点击跟换新的验证码
        protected void Button2_Click(object sender, EventArgs e)
        {
            //WebForm1 web = new WebForm1();
            //web.yzm();

            yzm();
           
        }

        //产生验证码的方法
        protected void yzm()
        {
            int x = new Random(DateTime.Now.Millisecond).Next(-9, 10);  //以当地时间的毫秒作为随机数的种子
            int y = new Random(DateTime.Now.Second).Next(-9, 10);   //以当地时间的秒作为随机数的种子


            int rnd = new Random().Next(0, 3);  //产生一个0-2的随机数
            switch (rnd)
            {
                case 0:

                    Label1.Text = x + "加上" + y + "等于";
                    Session["value"] = x + y;  //将验证码的值保持到Session中
                    break;
                case 1:

                    Label1.Text = x + "减去" + y + "等于";
                    Session["value"] = x - y;  //将验证码的值保持到Session中
                    break;
                case 2:

                    Label1.Text = x + "乘以" + y + "等于";
                    Session["value"] = x * y;  //将验证码的值保持到Session中
                    break;

            }
        }
    }
}

 
 

如何实现关闭浏览器后也能使用session?

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

namespace WebApp
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["userName"] = "张三";

            //虽然session会自动给我们创建一个cookie,在里面保存seesion的ID 。但是我们知道既然cookie里保存的sessionID那么我们就可以自己建立一个cookie,也在他里面保存session的ID,然后给这个session设置一个过期时间,让它变成一个持久cookie,这样即便关闭浏览器也能使用这个session了。注意:存在相同键的cookie,后者会覆盖前者
            //(注意:正常情况下session的ID保存在cookie里,而这个cookie是一个回话cookie,浏览器关闭,自动消失)

            //asp.net使用cookie保存session的id使用的键都是ASP.NET_SessionId.值是随机生成的
            HttpCookie hcSessionId = new HttpCookie("ASP.NET_SessionId", Session.SessionID);//将sessionId保存在cookie中
            hcSessionId.Expires = DateTime.Now.AddDays(7);
            Response.Cookies.Add(hcSessionId);
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值