业务系统实现记住密码和自动登录功能

业务系统实现记住密码和自动登录功能

  公司的业务系统本来是受域控的,用户不需要登录可以直接访问系统。当然,虽然不用人工登录,系统本身会读取电脑的用户名为登录标识,对系统操作权限和记录也是以电脑名。近段时间,由于系统要牵到云端,也就是不受域控了,那就需要每人手头上都有账号和密码了,这个和一般的业务系统没什么区别。但是由于用户之前的习惯是不用登录的,而且每天打开关闭的次数较多。OK,一般的系统登录都会有个记住密码的功能,但是,这还满足不了用户的需求,那么我们给用户增加多一个自动登录功能,类似QQ那样,我上次访问勾选了自动登录功能,然后再次访问就不用再去登录了。

  先来看看原来读取域用户的代码吧。

 string strname = HttpContext.Current.User.Identity.Name;//获取到带域名的用户
        string name = HttpContext.Current.User.Identity.Name.Substring(HttpContext.Current.User.Identity.Name.IndexOf("\\") + 1, HttpContext.Current.User.Identity.Name.Length - HttpContext.Current.User.Identity.Name.IndexOf("\\") - 1);

很多童鞋这样子获取到的是空的,是因为web.config没设置好。必须在配置文件<authentication mode="Windows">把mode的属性设为Windows,具体位置在<configuration><system.web>里面。

  好吧,下面就来实现我们的记住密码和自动登录功能吧。给大家看看登录界面吧。

先跟大家说说具体的功能吧

1、当用户勾选记住密码时,下次打开登录页面自动填充用户名和密码,用户只需点击登录按钮即可以登录,当然,你也可以删掉自动填充的密码自己重新输入,或者你用另外的用户密码登录也行。
2、当用户勾选自动登录时,记住密码也会自动被勾选上,因为只有记住了密码才会有自动登录,这是合理存在的。下次用户打开登录页面时,将会自动验证跳转到验证通过后的页面,不再需要用户去点击登录按钮。

 先看看前端的简要代码,简单点把需要用到的控件和js贴出来就好了,其它布局的就不贴出来了。js简单解释一下

<form 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>");
            }
        }


以上的注释只是我个人的一些思路和理解,如有不正确之处,还望大牛指导指导啊。如觉得文章对你有帮助,请多多支持,你们的支持将会是我写博客的动力。

 

  

 

 

  

posted @ 2013-10-11 10:35  knu  阅读(5255)  评论(4编辑  收藏  举报
©2009-2013 knu_knu. Version: 2.1.90 Release