標籤: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 使用者登陸時記住我