protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies["user"] != null) //Request.Cookies["车轮子"]
{
TextBox1.Text = Request.Cookies["user"].Values["id"].ToString();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie("user");
myCookie.Values["id"] = TextBox2.Text;
myCookie.Expires = DateTime.Now.AddHours(12); //不设这个找不到cookies文件
Response.Cookies.Add(myCookie);
}
测试程序如上,我发现cookie写操作正常能在Temporary Internet Files文件夹中看到写的内容。但当读时却读不出内容。
但把Button2_Click()的程序放在Page_Load()中Button1_Click()又可以读出cookie内容。
我指的是发布后在本机IE操作时出现的问题。在VS2008环境中调试一切正常。
什么好象页面重载了就读不出以前的值一样?
{
if (Request.Cookies["user"] != null) //Request.Cookies["车轮子"]
{
TextBox1.Text = Request.Cookies["user"].Values["id"].ToString();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie("user");
myCookie.Values["id"] = TextBox2.Text;
myCookie.Expires = DateTime.Now.AddHours(12); //不设这个找不到cookies文件
Response.Cookies.Add(myCookie);
}
测试程序如上,我发现cookie写操作正常能在Temporary Internet Files文件夹中看到写的内容。但当读时却读不出内容。
但把Button2_Click()的程序放在Page_Load()中Button1_Click()又可以读出cookie内容。
我指的是发布后在本机IE操作时出现的问题。在VS2008环境中调试一切正常。
什么好象页面重载了就读不出以前的值一样?
HttpCookie.Path为安全起见,浏览器是不会跨域去读取Cookie的
有关系
一般情况下,浏览器认为网站的目录是 /
如果你的网站放在虚拟目录下,比如 /VirtualPath/
,那么,asp.net生成的cookie的path就很可能是 /VirtualPath/
而浏览器认为 / 才是该站点的目录。
可是使用Firefox查看生成的Cookie的详细信息,这样便于调试。
你可以Google一下,有详细的说明!