简单登陆页

功能有:非空验证、记住我功能、验证码

记住我功能的["username"]和["pass"]这两个字段必须与数据库中的字段一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;


namespace lastOA
{
    public partial class denglu : System.Web.UI.Page
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

//记住我
                if (Request.Cookies["username"] != null)
                {
                    this.txtusername.Text = Request.Cookies["username"].Value;
                }
                if (Request.Cookies["username"] != null && Request.Cookies["pass"] != null)
                {
                    this.txtusername.Text = Request.Cookies["username"].Value;
                    string xx = Request.Cookies["pass"].Value;
                    txtpassword.Text = xx;
                }
 //记住我Load事件代码结束         
            }
        }
        protected void ibdenglu_Click(object sender, ImageClickEventArgs e)
        {
            if (this.txtusername.Text == null && this.txtpassword.Text == null && this.txtyzm.Text == null)
            {
                lbcuowu.Text = "用户名、密码、验证码不能为空";
            }
            else 
            {
                if (this.txtyzm.Text == Session["CheckCode"].ToString())//如果验证码校验通过则继续
                {
                    using (SqlConnection conn = new SqlConnection(constr))
                    {
                        conn.Open();
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = "select * from T_User where username=@username";
                            cmd.Parameters.AddWithValue("@username", txtusername.Text.Trim());
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    string username = reader.GetString(reader.GetOrdinal("username"));
                                    string pass = reader.GetString(reader.GetOrdinal("pass"));
                                    if (username == txtusername.Text.Trim())
                                    {
                                        if (pass == txtpassword.Text.Trim())
                                        {
                                          string leiname=  Getleiname(txtusername.Text);
                                          string name = Getname(txtusername.Text.Trim());
                                          //Response.Redirect("left.aspx?leiname="+leiname);
                                          Application["name"] = name;
                                          Application["leiname"] = leiname;
                                          date();//调用记住我方法
                                          Response.Redirect("Index.aspx");
                                            //Response.Write("<script>alert('"+aa+"')</script>");
                                        }
                                        else
                                        {
                                            lbcuowu.Text = "密码错误!";
                                        }
                                    }
                                }
                                else
                                {
                                    lbcuowu.Text = "帐号错误";
                                }
                            }


                        }
                    }
                }
                else
                {
                    //lbcuowu.Focus();
                    lbcuowu.Text = "验证码输入有误!";
                }
            }
            
        }
        private string Getname(string uname)
        {
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand cmd = conn.CreateCommand();
            conn.Open();
            cmd.CommandText = "select name from T_User where username=@uname";
            cmd.Parameters.AddWithValue("@uname", uname);
            string name = cmd.ExecuteScalar().ToString();
            return name;
        }
        private string Getleiname(string uname) 
        {
            SqlConnection conn = new SqlConnection(constr);
            SqlCommand cmd = conn.CreateCommand();
            conn.Open();
            cmd.CommandText = "select leiname from T_User where username=@uname";
            cmd.Parameters.AddWithValue("@uname",uname);
            string leiname = cmd.ExecuteScalar().ToString();
            return leiname;
        }
    //判断结束


//记住我方法
        private void date()
        {
            Response.Cookies["username"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["pass"].Expires = DateTime.Now.AddDays(-1);


            if (CheckBox1.Checked)
            {
                Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);
                Response.Cookies["pas"].Expires = DateTime.Now.AddDays(7);
            }


            Response.Cookies["username"].Value = this.txtusername.Text.Trim();
            Response.Cookies["pass"].Value = this.txtpassword.Text.Trim();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值