Cookie操作
1、写cookie方法
#region 登录时写入cookie public static void LoginCookieSave(string uid, string loginname, string pwd) { System.Web.HttpCookie logincookie = new System.Web.HttpCookie("userinfo"); logincookie.Values.Add("uid", uid); logincookie.Values.Add("loginname", ICore.IChecking.Escape(loginname)); logincookie.Values.Add("pwd", pwd); logincookie.Expires = DateTime.Now.AddMonths(12); logincookie.Domain = "domain.com"; HttpContext.Current.Response.AppendCookie(logincookie); } #endregion
2、读取cookie方法
/// <summary> /// 获得Cookie的值 /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <returns></returns> public static string GetCookieValue(string cookieName, string key) { HttpRequest request = HttpContext.Current.Request; if (request != null) return GetCookieValue(request.Cookies[cookieName], key); return ""; } /// <summary> /// 获得Cookie的子键值 /// </summary> /// <param name="cookie"></param> /// <param name="key"></param> /// <returns></returns> public static string GetCookieValue(HttpCookie cookie, string key) { if (cookie != null) { if (!string.IsNullOrEmpty(key) && cookie.HasKeys) return cookie.Values[key]; else return cookie.Value; } return ""; }