asp.net Cookie 使用者登陸時記住我

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.