Cookie的写入与读出

2017-09-20  本文已影响0人  Oh宝贝儿

做一个登录页,想要保存用户的密码,这样就不用每次都输入密码了,比较方便, 刚接触cookie,不太懂,自己折腾了

两天,终于弄出个简单的出来,不过还没有给cookie加密,后期再补上吧。下面是代码 ,记在这里,以防自己以后忘记。

1.Login.aspx代码 :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         用户名:<asp:TextBox runat="server" ID="txtUserName"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="rfvUserName" ControlToValidate="txtUserName"
 ErrorMessage="用户账号不能为空,请输入您的用户账号!"></asp:RequiredFieldValidator>
        <br/>
        密码:<asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="rfvPassword" ControlToValidate="txtPassword"
 ErrorMessage="用户密码不能为空,请输入您的密码!" Display="Dynamic"></asp:RequiredFieldValidator>
        <br/>
        <asp:CheckBox ID="SavePwd" runat="server" Text="保存密码"  
              />
        <br/>
        <asp:Button runat="server" ID="btnLogin" Text="登录" onclick="btnLogin_Click"/>
        <br />
    </div>
    </form>
</body>
</html>

2.Login.aspx.cs代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ESchool.BLL;
using ESchool.Entity;
using System.Collections.Generic;

using System.Data;



public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //读取cookiek 保存的用户信息值
            if (Request.Cookies["LoginInfo"] != null)
            {
                //将本地保存的用户名写入页面的对应的textbox中。
                this.txtUserName.Text = Request.Cookies["LoginInfo"]["userName"];
                //将本地保存的用户密码名写入页面的对应的textbox中。
                this.txtPassword.Attributes.Add("value", Request.Cookies["LoginInfo"]["userPassword"].ToString());
                
                
                //或
                //System.Collections.Specialized.NameValueCollection UserInfoCookieCollection;
                //UserInfoCookieCollection = Request.Cookies["LoginInfo"].Values;
                //this.txtUserName.Text =
                //    Server.HtmlEncode(UserInfoCookieCollection["userName"]);
                //this.txtPassword.Text =
                //    Server.HtmlEncode(UserInfoCookieCollection["userPassword"]);
            }
        }

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
       
        //将用户名保存到 cookie
        Response.Cookies["LoginInfo"]["userName"] = this.txtUserName.Text.Trim();
        //判断是否将密码保存到 cookie
        if (SavePwd.Checked)
        {
            Response.Cookies["LoginInfo"]["userPassword"] = this.txtPassword.Text.Trim();
        }


        //密码保存时间
        Response.Cookies["LoginInfo"].Expires = DateTime.Now.AddMinutes(2);


        //或
        //HttpCookie userCookie = new HttpCookie("LoginInfo");
        //userCookie.Values["userName"] = this.txtUserName.Text.Trim();
        //if (this.SavePwd.Checked)
        //{
        //    userCookie.Values["userPassword"] = this.txtPassword.Text.Trim();

        //}

        //userCookie.Expires = DateTime.Now.AddDays(2);
        //Response.Cookies.Add(userCookie);
      
    }

   
}

写这个的时候也遇到了不少问题,首先就是在第一次运行时,保存密码后再次进入这个页面时,发现密码没有保存,

只保存了用户名,以为自己代码有错呢,检查了半天,也上网查了好多,发现没有什么不妥的。几经折腾,在一步一步调试中发现了问题,保存用户名和密码的语句是一样的,能保存用户名就应该保存密码,那么能读出用户名也就应该能读出密码,调试到

  //将本地保存的用户密码名写入页面的对应的textbox中。
                this.txtPassword.Attributes.Add("value", Request.Cookies["LoginInfo"]["userPassword"].ToString());
                


时,发现,密码已经读出来了,可是为什么不能显示在文本框中???纳闷了,看了网上一些资料,发现了好多 人都有这样的问题。

问题在于TextMode="Password",这样的话就会把cookie中读出来密码给屏蔽掉,造成不显示密码。

//将本地保存的用户名写入页面的对应的textbox中。
                this.txtUserName.Text = Request.Cookies["LoginInfo"]["userName"];
                //将本地保存的用户密码名写入页面的对应的textbox中。
                this.txtPassword.Attributes.Add("value", Request.Cookies["LoginInfo"]["userPassword"].ToString());


把显示密码的语句改成上面的形式就可以显示密码了,还没有完全搞清楚 为什么要这样写。研究中,希望我的这些

能对大家有点帮助。

原文出处:http://m.blog.csdn.net/itmaxin/article/details/7667201

上一篇 下一篇

猜你喜欢

热点阅读