一、session
Session["checkcode"] = "";
String checkcode = Session["checkcode"];
//保存对象
Agent agent = new Agent();
Session["agent"] = agent;
//取值
Agent agent = (Agent)Session["agent"];
二、cookie 的应用
HttpCookie cookie = new HttpCookie("mycookie"); //定义cookie对象以及名为mycookie的项
DateTime dt = DateTime.Now;//定义时间对象
TimeSpan ts=new TimeSpan(1,0,0,0); //cookie有效作用时间,具体查msdn
cookie.Expires = dt.Add(ts); //添加作用时间
cookie.Values.Add("user","you name"); //增加属性
cookie.Values.Add("userid","123456");
Response.AppendCookie(cookie); //确定写入cookie中 读取cookie
if(Request.Cookies["mycookie"]!=null)
{
string temp=Convert.ToString(Request.Cookies["mycookie"].Values["user"])+" "+
Convert.ToString(Request.Cookies["mycookie"].Values["userid"]);
//读全部就用Request.Cookies["mycookie"].Value)
}
修改cookie
Response.Cookies["mycookie"]["user"] = "2";
Response.Cookies["mycookie"].Expires = DateTime.Now.AddDays(1); 删除cookie下的属性
HttpCookie acookie=Request.Cookies["mycookie"];
acookie.Values.Remove("userid");
acookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(acookie); //删除所有cookie,就是设置过期时间为现在就行了
int limit=Request.Cookies.Count - 1;
for(int i=0;i<limit;i++)
{
acookie = Request.Cookies(i)
acookie.Expires = DateTime.Now.AddDays(-1)
Response.Cookies.Add(acookie)
}