码迷,mamicode.com
首页 > Web开发 > 详细

asp.net Cookie 用户登陆时记住我

时间:2014-11-08 00:47:34      阅读:381      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   sp   for   数据   

 

        /// <summary>
        /// 判断Cookie中存储的数据
        /// </summary>
        protected void CheckUserCookie()
        {
            //先判断Cookie是否有值
            if (Request.Cookies["cp1"] != null && Request.Cookies["cp2"] != null)
            {
                //校验数据是否正确.
                string cookieUserName = Request.Cookies["cp1"].Value;
                string cookieUserPwd=Request.Cookies["cp2"].Value;
                BLL.UserManager bll = new BLL.UserManager();
                Model.User model=bll.GetModel(cookieUserName);//根据Cookie中存储的用户名找用户
                if (model != null)
                {
                    //判断密码是否正确.
                    //如果注册时,采用相同的加密方式,那么这里再比较时直接比较
                    if (Enctry(model.LoginPwd) == cookieUserPwd)
                    {
                        Session["UserInfo"] = model;
                        GoPage("登录成功");
                    }
                    else//表示密码错误,删除Cookie
                    {
                        Response.Cookies["cp1"].Expires = DateTime.Now.AddDays(-1);
                        Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1);
                    }
                }
            }
        }
        /// <summary>
        /// 校验用户登录信息
        /// </summary>
        protected void CheckUserLogin()
        {
            string txtName=Request.Form["txtName"];
            string txtPwd=Request.Form["txtPwd"];
            BLL.UserManager bll = new BLL.UserManager();
            string msg = string.Empty;
            Model.User model = null;
            //校验用户名密码
           bool b= bll.UserLogin(txtName, txtPwd,out msg,out model);
           if (b)
           {
               Session["UserInfo"] = model;
               //如果选择了记住我复选框,将用户的信息写到Cookie。
               if (!string.IsNullOrEmpty(Request.Form["checkMe"]))
               {
                   HttpCookie cookie1 = new HttpCookie("cp1", model.LoginId);
                   HttpCookie cookie2 = new HttpCookie("cp2",Enctry(model.LoginPwd));
                   cookie1.Expires = DateTime.Now.AddDays(3);
                   cookie2.Expires = DateTime.Now.AddDays(3);
                   Response.Cookies.Add(cookie1);
                   Response.Cookies.Add(cookie2);
               }
               GoPage(msg);
          
           }
           else
           {
               Response.Redirect("/ShowMsg.aspx?msg=" + Server.UrlEncode(msg) + "&txt=" + Server.UrlEncode("登录页") + "&url=/Login.aspx");
           }
        }

 

asp.net Cookie 用户登陆时记住我

标签:style   blog   http   io   color   ar   sp   for   数据   

原文地址:http://www.cnblogs.com/han1982/p/4082555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!