CooieHelper类,方法 清除cookie退出登录


/// <summary> /// 为HttpCookie创建kye,hash存cookie对象的key和值 /// </summary> /// <param name="strKey"></param> /// <param name="hash"></param> public static void AddCookie(string strKey, Hashtable hash) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strKey]; if (cookie != null) { cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookie); } if (hash != null) { cookie = new HttpCookie(strKey); foreach (DictionaryEntry de in hash) { cookie.Values.Add(de.Key.ToString(), de.Value.ToString()); } cookie.Expires = DateTime.Now.AddHours(5); HttpContext.Current.Response.Cookies.Add(cookie); } } /// <summary> /// 判断cook对象是否为空 /// </summary> /// <param name="strKey"></param> /// <returns></returns> public static HttpCookie GetCookie(string strKey) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strKey]; if (cookie != null) { return cookie; } else { return null; } }

/// <summary>
///推出清除Cookie
/// </summary>
protected void LinkExit_Click(object sender, EventArgs e)
{

    HttpCookie cookie = Request.Cookies["employee"];

if (cookie != null)
{
cookie.Expires = DateTime.Today.AddDays(-1);
Response.Cookies.Add(cookie);
}
Response.Redirect("../Login.aspx");

}

 

posted @ 2012-05-30 11:35  程序猿网友666  阅读(460)  评论(0编辑  收藏  举报