asp.net Cookie 用戶登陸時記住我


 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM