关于 Cookie 的装箱和拆箱
写入 Cookie
private void Button4_Click(object sender, System.EventArgs e)
{
HttpCookie cookie = new HttpCookie( "tosinUser" );
cookie.Values.Add( "user", "wang" );
cookie.Values.Add( "password", "000711" );
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan( 0, 1, 0, 0);
cookie.Expires = dt.Add( ts );
Response.Cookies.Add( cookie );
}
读取 Cookie
private void Button5_Click(object sender, System.EventArgs e)
{
HttpCookie cookie = Request.Cookies["tosinUser"];
cookie.Value = cookie.Value; //这里本来是解密语句,为了简便这样写的
TextBox2.Text = cookie.Values["password"].ToString();//这里出错,说未初始化对象,应该是找不到,请问怎么处理呢? 是用 装箱吗? 我装了也不行啊
}