我在page_load里建立一个cookie 并设置过期时间为6秒 但是它确总也不过期
各位帮忙
private void Page_Load(object sender, System.EventArgs e)
{
HttpCookie c=new HttpCookie( "UserInfo1 ");
c.Domain= "myDomain ";
c.Values.Add( "u ", "li ");
c.Values.Add( "host ", "liye "); c.Expires=DateTime.Now.AddSeconds(6);
Response.AppendCookie(c);
}
单击按钮时,读取cookie内容 发送到页面,即使过了6秒 或者 6秒后重新登录依然能读取cookie
private void Button1_Click(object sender, System.EventArgs e)
{
string s1= "no ";
string s2= "no ";
HttpCookie c=Request.Cookies[ "UserInfo1 "];
if(c!=null)
{
s1=c.Values[ "u "].Trim();
s2=c.Values[ "host "].Trim();
}
Response.Write( "u= "+s1+ "host= "+s2);
}
为什么?
------解决方案--------------------------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
..............
}
}
------解决方案--------------------------------------------------------
用你以前的代码 也没事 过一会儿 就在 地址栏输入
javascript:alert(window.document.cookie[ "UserInfo1 "].value);