asp.net 保存cookies

2024-12-01 19:29:43
推荐回答(3个)
回答1:

//----------写入Cookie
HttpCookie cookie = new HttpCookie("userinfo1");
cookie.Expires = System.DateTime.Now.AddMinutes(2);//设置过期时间
for (int i = 0; i < 20; i++)
{

cookie.Values["Val" +i.ToString()] = i.ToString();
Response.Cookies.Add(cookie);
}

//读取 Cookie 集合
for (int i = 0; i < Request.Cookies.Count; i++)
{
if (Request.Cookies.AllKeys == "userinfo1")
{

HttpCookie cookies = Request.Cookies["userInfo1"];
Response.Write("name=" + cookies.Name + "
");
//Response.Write("name=" + cookies.Value + "
");

if (cookies.HasKeys)//是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl = cookies.Values;
for (int j = 0; j < NameColl.Count; j++)
{
Response.Write("子键名=" + NameColl.AllKeys + "
");
Response.Write("子键值=" + NameColl + "
");
}

}
else
{
Response.Write("value=" + cookies.Value + "
");
}
}

}

回答2:

我知道 想要答案加分

回答3:

添加过期时间