登陆,验证码,记住我

前台:

 <div>
    <asp:Label ID="Label1" runat="server" Text="账号:"></asp:Label>
        <asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
        <br />
         <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
        <asp:TextBox ID="txtpass" runat="server"></asp:TextBox>
        <br />
        <br />
      
验证码:<img src="YanZheng.aspx" id="YanZheng" alt="" style="cursor:hand" hspace="0" align="top" />&nbsp;

 <a href="javascript:document.getElementById('YanZheng').src='YanZheng.aspx?'+new Date;void(0);">换一张?</a>
<br />
        输入验证码<asp:TextBox ID="txtYanZheng" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnlg" runat="server" Text="Button" οnclick="btnlg_Click" />
        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:Label ID="Label3" runat="server" Text=""></asp:Label>

        <asp:Label ID="Label4" runat="server" Text=""></asp:Label>
    </div>

 

后台:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["UserName"]!=null)
                {
                    this.txtuser.Text = Request.Cookies["UserName"].Value;
                }
                if (Request.Cookies["UserName"]!=null&&Request.Cookies["Password"]!=null)
                {
                    this.txtuser.Text = Request.Cookies["UserName"].Value;
                    this.txtpass.Text = Request.Cookies["Password"].Value;
                }
            }
        }

        protected void btnlg_Click(object sender, EventArgs e)
        {
            DataLoad();
            data();
        }
        private void DataLoad()
        {
            if (txtYanZheng.Text.Equals(Session["CheckCode"]+""))
            {
                string contor = ConfigurationManager.ConnectionStrings["stcron"].ConnectionString;
                SqlConnection coon = new SqlConnection(contor);
                coon.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = coon;
                cmd.CommandText = "select UserName,Password,UserId from T_User where UserName=@username ";
                cmd.Parameters.AddWithValue("@username", txtuser.Text);
                SqlDataAdapter adta = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adta.Fill(dt);
                if (dt.Rows.Count>0)
                {
                    string username = dt.Rows[0]["UserName"].ToString();
                    string pass = dt.Rows[0]["Password"].ToString();
                    if (txtuser.Text==username&&txtpass.Text==pass)
                    {
                        string userid = dt.Rows[0]["UserId"].ToString();
                        Session["Id"] = userid;
                        Session["name"] = username;

                        Response.Redirect("接受.aspx");
                    }
                    else if (txtuser.Text==username&&txtpass.Text!=pass)
                    {
                        Label3.Text = "密码错误";
                    }
                }
                else
                {
                    Label3.Text = "用户不存在";
                }
            }
            else
            {
                Label4.Text = "验证码不对";
            }
        }
        private void data()//记住我
        {
            Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
            if (CheckBox1.Checked)
            {
                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(7);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(7);
            }
            Response.Cookies["UserName"].Value = this.txtuser.Text.Trim();
            Response.Cookies["Password"].Value = this.txtpass.Text.Trim();
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值