1.添加cookies(用cookies方式去做sso,用户信息保存,修改都会依赖cookies)
#region##添加cookeis ///<summary> /// 添加cookeis ///</summary> public void AddCookies() { HttpCookie cookies = new HttpCookie("Porschev"); cookies["name"] = "Zhong Wei"; cookies["sex"] = "1"; cookies.Expires = DateTime.Now.AddMinutes(20); Response.Cookies.Add(cookies); } #endregion添加cookies一般不会有人出错的。。。记得添加Expires就行
#region##修改cookies ///<summary> /// 修改cookies ///</summary> public void ModCookies() { HttpCookie cookies = Request.Cookies["Porschev"]; cookies["name"] = "wilson Z"; } #endregion新建个测式页。。再取cookies中的name,结果:wilson Z;
#region##修改cookies
///<summary>
/// 修改cookies
///</summary>
public void ModCookies()
{
HttpCookie cookies = Request.Cookies["Porschev"];
cookies["name"] = "wilson Z";
//加上下面一句 Response.Cookies["Porschev"].Expires = DateTime.Now.AddMinutes(-1);
}
#endregion
再次测式,结果正确,取出name的值为:wilson Z,页面显示也正确
#region##得到cookies ///<summary> /// 得到cookies ///</summary> public void GetCookies() { HttpCookie cookies = Request.Cookies["Porschev"]; string name = cookies["name"]; //通过key取出对应value,多key同理取 } #endregion
#region##删除cookies ///<summary> /// 删除cookies ///</summary> public void DelCookeis() { if (Request.Cookies["Porschev"] != null) { HttpCookie cookies = new HttpCookie("Porschev"); cookies.Expires = DateTime.Now.AddDays(-1); Response.Cookies.Add(cookies); } } #endregion
#region##删除cookies ///<summary> /// 删除cookies ///</summary> public void DelCookeis() { foreach (string cookiename in Request.Cookies.AllKeys) { HttpCookie cookies = Request.Cookies[cookiename]; if (cookies != null) { cookies.Expires = DateTime.Today.AddDays(-1); Response.Cookies.Add(cookies); Request.Cookies.Remove(cookiename); } } } #endregion测式:删除成功
自己理解:方法一只能删除提当前Response中的cookies,不对完成删除掉客端的cookies,用遍历cookies的方式可以方法一定对,己经过项目实际验证,观点不一定正确,如理解有误,请各位指正!!