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方法
////// 获得Cookie的值 /// /// /// ///public static string GetCookieValue(string cookieName, string key) { HttpRequest request = HttpContext.Current.Request; if (request != null) return GetCookieValue(request.Cookies[cookieName], key); return ""; } /// /// 获得Cookie的子键值 /// /// /// ///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 ""; }