Cookie是小段保存在客户端的数据,一般保存在"我的文档\Cookies文件夹下",所以这个特性让Cookie的安全性弱,可以被伪造!.
关于如何伪造Cookie可以查看这篇文章:
当用户访问网站时,浏览器会先根据网站的URL在本地Cookie文件夹内查找是否有相同关联的Cookie,有的话就连同页面一起发送到服务器!.
如果浏览器禁止了Cookie,那么浏览器将不会再查找相关Cookie,而且也写入不了Cookie!.
大多数浏览器规定Cookie的大小不超过4k(我一般用Cookie也存不了这多东西吧),每个站点能保存的Cookie不超过20个!.所有站点保存的Cookie总和不超过300个!.所以有时候你就算不禁止Cookie,也会因为浏览器保存的Cookie满了,而导致失败(会导致Session失效哦
),
所以我们一般不要在Cookie上存放关键数据,当然,用Cookie验证的话要和Session配合一起验证(
如果禁止Cookie,Session也失效了,那就会发生登陆不了的事)!.
在asp时,我们读Cookie写Cookie就用Request.Cookie和Response.Cookie!.
在asp.Net里使用方法!.
单值Cookie:
//*****************************设置Cookie的值*********************************
HttpCookie alwaysthere = new HttpCookie("URL","http://www.alwaysthere.com.cn");
alwaysthere.Expires = DateTime.Now.AddDays(1); //设置失效日期
Response.Cookies.Add(alwaysthere);
//*****************************读取单值Cookie的值******************************
HttpCookie alwaysthere = Request.Cookies["URL"];
if (alwaysthere != null)
{
Response.Write(string.Format("keyL{0} Value:{1}","URL",alwaysthere.Value))
}
多值Cookie:
HttpCookie alwaysthere = new HttpCookie("URL");
alwaysthere.Values["key1"] = "title";
alwaysthere.Values["key2"] = "Name";
alwaysthere.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(alwaysthere);
//*****************************读取多值Cookie的值******************************
HttpCookie alwaysthere = Request.Cookies["URL"];
if (alwaysthere != null)
{
Response.Write(string.Format("Key:{0} Value:{1}<br />,"URL",alwaysthere.Value"));
foreach (string subkey in alwaysthere.Values.AllKeys)
{
Response.Write(string.Format("SubKey:{0} Value:{1} <br />",subkey,alwaysthere.Values[subkey]));
}
}
//*******************************删除Cookie*********************************
HttpCookie alwaysthere = Request.Cookies["URL"];
alwaysthere.Expires = DateTime.MinValue;
Response.Cookies.Add(alwaysthere);
//******************************删除所有Cookie******************************
foreach (string key in Request.Cookies.Allkeys)
{
HttpCookie alwaysthere = Request.Cookies[key];
alwaysthere.Expires = DataTime.MinValue;
Response.Cookies.Add(alwaysthere);
}
//*******************************修改Cookie***********************************
HttpCookie alwaysthere = Request.Cookies["URL"];
alwaysthere.Value = "修改后的值-always there's Blog";
Response.Cookies.Add(alwaysthere);