WEB页面代码:
<form id="Form1" runat="server"> <input type="hidden" id="hidPass" runat="server" /> <input type="text" runat="server" id="txtLoginName" /> <asp:TextBox runat="server" ID="txtPassWord" class="textwidthheigh" TextMode="Password"></asp:TextBox> <input type="checkbox" runat="server" value="记住密码" id="chkRemember" onclick="CheckRemember()" /> <input type="checkbox" runat="server" value="自动登录" id="chkLogin" onclick="CheckLogin()" /> <asp:Button runat="server" id="btnLogin" onclick="btnLogin_Click" /> <input type="button" id="btnClear" onclick="Clear()" /> </form> <script type="text/javascript" language="javascript"> //页面加载用户名输入框获得焦点 document.getElementById("txtLoginName").focus(); function Clear() { //用户点击取消,清空用户名和用户密码 document.getElementById("txtLoginName").value = ""; document.getElementById("txtPassWord").value = ""; } function CheckLogin() { //用户勾选自动登录时,把记住密码也勾选上 var remember = document.getElementById("chkRemember"); remember.checked = true; } function CheckRemember() { var remenber = document.getElementById("chkRemember"); var login = document.getElementById("chkLogin"); if (remenber.checked == false) { login.checked = false; //用户去掉记住密码时,也把自动登录去掉 } } </script>
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //获取客户端的Cookies,分别两个cookies,一个登陆名,一个密码 HttpCookie LoginNameCookie = Request.Cookies["Bic_LoginName"]; HttpCookie LoginPassCookie = Request.Cookies["Bic_Pass"]; if (LoginNameCookie != null) { //登录名的cookies不为空,填充登陆名 txtLoginName.Value = LoginNameCookie.Value; } if (LoginPassCookie != null) { //密码cookies不为空,给密码框和隐藏密码框填充,当然我们的密码是加密过才存到cookies去的,至于以藏文本框的作用后面就会看到 this.txtPassWord.Attributes.Add("value", LoginPassCookie.Value + ""); hidPass.Value = LoginPassCookie.Value + "";//赋值给隐藏控件 chkRemember.Checked = true; } //获取是否有勾选自动登录的cookies HttpCookie Login = Request.Cookies["Bic_LoginAuto"]; //当用户在系统点击退出时 if (Request["opFlag"] == "Exit") { this.txtPassWord.Attributes.Add("value", "");//把密码去掉 chkRemember.Checked = false;//记住密码去掉 HttpCookie loginNameCookie = Request.Cookies["Bic_LoginName"];//获取登录名cookies HttpCookie loginPassCookie = Request.Cookies["Bic_Pass"];//获取密码cookies if (loginNameCookie != null) { //把cookies时间设为-2相当于删掉了cookies loginNameCookie.Expires = DateTime.Now.AddDays(-2); Response.Cookies.Set(loginNameCookie); } if (loginPassCookie != null) { //把密码的cookies也删掉 loginPassCookie.Expires = DateTime.Now.AddDays(-2); Response.Cookies.Set(loginPassCookie); } //自动登录cookies也一样 HttpCookie login = Request.Cookies["Bic_LoginAuto"]; if (login != null) { login.Expires = DateTime.Now.AddDays(-2); Response.Cookies.Set(login); } } else//用户打开登录界面时 { //自动登录cookies不为空,用户名不为空,隐藏框密码不为空 if (Login != null && txtLoginName.Value != "" && hidPass.Value != "") { SysUser user = new SysUser(); user.Login_Name = txtLoginName.Value; user.Login_Pass = hidPass.Value; int i = SysUserBLL.Login(user);//验证登录 if (i > 0) { //成功登录跳转到default.aspx页面 Page.Session["Login_Name"] = user.Login_Name; HttpContext.Current.Session["Display_Name"] = SysUserBLL.getDisplayNameByname(user.Login_Name); Response.Write("<script>window.location=‘Default.aspx‘;</script>"); Response.End(); } } } } } //点击登录按钮事件 protected void btnLogin_Click(object sender, EventArgs e) { //判断是否为空 if (txtLoginName.Value.Trim() != "" && txtPassWord.Text.Trim() != "") { SysUser user = new SysUser(); user.Login_Name = txtLoginName.Value.Trim(); user.Login_Pass = CommonHelper.MD5encipher(txtPassWord.Text.Trim());//MD5加密 HttpCookie LoginNameCookie = Request.Cookies["Bic_LoginName"]; HttpCookie LoginPassCookie = Request.Cookies["Bic_Pass"]; if (LoginNameCookie != null)//如果是记住密码情况 { if (txtLoginName.Value.Trim() == LoginNameCookie.Value.Trim())//读取到cookies保存的用户名和文本框用户名相同,预防用户又改动 { if (LoginPassCookie != null) { if (txtPassWord.Text.Trim() == LoginPassCookie.Value.Trim())//cookies读取到的密码和文本框密码相同 { user.Login_Pass = txtPassWord.Text.Trim(); } } } } int i = SysUserBLL.Login(user);//验证登录 if (i > 0) { if (chkRemember.Checked == true)//记住密码 { HttpCookie loginNameCookie = new HttpCookie("Bic_LoginName", user.Login_Name); HttpCookie loginPassCookie = new HttpCookie("Bic_Pass", user.Login_Pass); loginPassCookie.Expires = DateTime.Now.AddDays(1); loginNameCookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(loginNameCookie); Response.Cookies.Add(loginPassCookie); if (chkLogin.Checked == true)//自动登录 { HttpCookie Login = new HttpCookie("Bic_LoginAuto", "true"); Login.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(Login); } else { HttpCookie Login = Request.Cookies["Bic_LoginAuto"]; if (Login != null) { Login.Expires = DateTime.Now.AddDays(-2); Response.Cookies.Set(Login); } } } else//没选记住密码 { HttpCookie loginNameCookie = Request.Cookies["Bic_LoginName"]; HttpCookie loginPassCookie = Request.Cookies["Bic_Pass"]; if (loginNameCookie != null) { loginNameCookie.Expires = DateTime.Now.AddDays(-2); Response.Cookies.Set(loginNameCookie); } if (loginPassCookie != null) { loginPassCookie.Expires = DateTime.Now.AddDays(-2); Response.Cookies.Set(loginPassCookie); } } Page.Session["Login_Name"] = user.Login_Name; HttpContext.Current.Session["Display_Name"] = SysUserBLL.getDisplayNameByname(user.Login_Name); Response.Write("<script>window.location=‘Default.aspx‘;</script>"); } else { Response.Write("<script>alert(‘用户名或密码错误!‘);window.location=‘Login.aspx‘;</script>"); } } else { Response.Write("<script>alert(‘请输入账号和密码!‘);window.location=‘Login.aspx‘;</script>"); } }