Hopes

          Start Here..

           

          使用COOKIE對(duì)像實(shí)現(xiàn)保存用戶基本信息(結(jié)合Session),ASP.Net實(shí)現(xiàn)用戶登錄全過程

          ///使用COOKIE對(duì)像實(shí)現(xiàn)保存用戶基本信息(結(jié)合
          Session),ASP.Net實(shí)現(xiàn)用戶登錄全過程 

          //1.創(chuàng)建一個(gè)WEB用戶控件頁面

          。添加兩個(gè)HTml 客戶端DIV 并專為服務(wù)控件 前臺(tái)代碼如下

          <div id="divLogin" runat="server"> ……</div>

          <div id="divMain" runat="server" > ……</div>

          。在div divLogin 中添加 Label(用戶名) textBox(用戶名) Label( 密碼) textBOx(密碼) Checkbox(記住我) button(登入)

          。在div DivMain 中 添加 Label(HI,) Label(XXX)

          //2.WEB用戶控件的后臺(tái)代碼實(shí)現(xiàn) 

          protected void Page_Load(object sender, EventArgs e)
          {
          //判斷是否有Cookie信息可讀
          if (Request.Cookies.Get("loginUserName"!= null)
          {
          //將Cookie中的登錄信息讀取出來保存到Session["loginUser"]中
          Session["loginUser"= Request.Cookies["loginUserName"].Value;
          }



          //通過判斷Session["loginUser"]的值是否為空位判斷是否登陸過,以及顯示哪一個(gè)Div
          if (Session["loginUser"== null)
          {
          this.divLogin.Visible = true;
          this.divMain.Visible = false;
          }
          else
          {
          this.divLogin.Visible = false;
          this.divLoginMain.Visible = true;
          this.lblUserMessage.Text = Session["loginUser"].ToString();
          }
          }
          protected void btnLogin_Click(object sender, EventArgs e) //登錄按鈕Click事件
          {
          //保存登陸信息到Session["loginUser"] 
          if (ValidateUser(this.txtID.Text, this.txtPWD.Text))
          {
          Session[
          "loginUser"= this.txtID.Text;
          //如果選擇了保存Cookie
          if (this.chkIsSaveCookie.Checked)
          {
          HttpCookie cookie 
          = new HttpCookie("loginUserName",this.txtID.Text);
          cookie.Expires 
          = DateTime.Now.AddMonths(1);
          Response.Cookies.Add(cookie);
          }
          }
          //刷新當(dāng)前頁面
          Response.Redirect(Request.Url.ToString());
          }



          /// <summary>
          /// 這里省略了訪問數(shù)據(jù)庫驗(yàn)證的過程
          /// </summary>
          /// <param name="id">登陸賬號(hào)</param>
          /// <param name="pwd">登陸密碼</param>
          /// <returns></returns>
          public bool ValidateUser(string id, string pwd) //該方法用于數(shù)據(jù)驗(yàn)證,此處略
          {
          return true;
          }

          protected void btnSafeExit_Click(object sender, EventArgs e) //安全退出按鈕Click事件
          {
          //清空Session中的用戶信息
          Session["loginUser"= null;
          //將本地Cookie中的信息移除----讓Cookie過期
          HttpCookie cookie = Request.Cookies["loginUserName"];
          if (cookie != null)
          {
          cookie.Expires 
          = DateTime.Now.AddDays(-10);
          Response.Cookies.Add(cookie);
          }



          //刷新當(dāng)前頁面
          Response.Redirect(Request.Url.ToString());



          }


          標(biāo)簽: Cookie
            

          posted on 2012-09-16 13:55 ** 閱讀(1427) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          你好!

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊(cè)

          收藏夾

          C#學(xué)習(xí)

          友情鏈接

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 梁山县| 贵州省| 日喀则市| 龙山县| 余干县| 金阳县| 永德县| 新乡县| 耒阳市| 高雄县| 苍溪县| 县级市| 称多县| 杂多县| 屏东县| 岑巩县| 平南县| 姜堰市| 呼和浩特市| 罗源县| 黔南| 桂阳县| 平塘县| 龙游县| 时尚| 揭东县| 沙河市| 宁南县| 武邑县| 宁明县| 札达县| 保德县| 襄樊市| 东乡| 宝山区| 黄山市| 宜昌市| 衡山县| 资中县| 海丰县| 佛山市|