/// <summary> /// 生成一个多键cookie. /// </summary> /// <param name="cookieName">cookie 文件名</param> /// <param name="arrKey">cookie的键数组</param> /// <param name="arrValue">键值数组</param> /// <param name="request"></param> /// <param name="response"></param> /// <param name="server"></param> /// <returns>生成成功则返回真, 否则返回假</returns> public static bool GenCookie(string cookieName, string[] arrKey, string[] arrValue, System.Web.HttpRequest request, System.Web.HttpResponse response, System.Web.HttpServerUtility server) { if (CheckCookie(cookieName, request) == true) { if (!RemoveCookie(cookieName, request, response)) { return(false); } } if (arrKey.Length != arrValue.Length) { return(false); } System.Web.HttpCookie objCookie = new System.Web.HttpCookie(cookieName); for (int i = 0; i < arrKey.Length; i++) { objCookie.Values[arrKey[i]] = server.UrlEncode(arrValue[i]); } try { response.AppendCookie(objCookie); } catch (Exception ex) { return(false); } return(true); }
/// <summary> /// 生成一个单键 cookie. 如果指定的名称已存在, 则删除原有cookie. /// </summary> /// <param name="cookieName">要生成的cookie名 </param> /// <param name="value">键值 </param> /// <param name="request"></param> /// <param name="response"></param> /// <param name="server"></param> /// <returns>生成成功则返回真, 否则返回假.</returns> public static bool GenCookie(string cookieName, string value, System.Web.HttpRequest request, System.Web.HttpResponse response, System.Web.HttpServerUtility server) { if (CheckCookie(cookieName, request) == true) { if (!RemoveCookie(cookieName, request, response)) { return(false); } } System.Web.HttpCookie objCookie = new System.Web.HttpCookie(cookieName); objCookie.Value = server.UrlEncode(value); try { response.AppendCookie(objCookie); } catch (Exception ex) { return(false); } return(true); }
/// <summary> /// 删除一个cookie. /// </summary> /// <param name="cookieName">cookie名</param> /// <param name="request"></param> /// <param name="response"></param> /// <returns>如果成功删除, 返回真, 否则返回假</returns> public static bool RemoveCookie(string cookieName, System.Web.HttpRequest request, System.Web.HttpResponse response) { if (CheckCookie(cookieName, request) == false) { return(true); } System.Web.HttpCookie objCookie = request.Cookies[cookieName]; objCookie.Expires = DateTime.Now.AddMinutes(-1); try { response.AppendCookie(objCookie); } catch (Exception ex) { return(false); } return(true); }
/// <summary> /// 修改一个多键cookie的值. /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="request"></param> /// <param name="response"></param> /// <param name="server"></param> /// <returns></returns> public static bool ChangeKeyValue(string cookieName, string key, string value, System.Web.HttpRequest request, System.Web.HttpResponse response, System.Web.HttpServerUtility server) { if (CheckCookie(cookieName, request) == false) { return(false); } try { System.Web.HttpCookie objCookie = request.Cookies[cookieName]; objCookie.Values[key] = server.UrlEncode(value); response.AppendCookie(objCookie); } catch (Exception ex) { return(false); } return(true); }
/// <summary> /// 设置一个cookie的过期时间 /// </summary> /// <param name="cookieName"></param> /// <param name="dtObsolete"></param> /// <param name="request"></param> /// <param name="response"></param> /// <returns>设置成功返回真, 否则返回假</returns> public static bool SetCookieExpire(string cookieName, DateTime dtObsolete, System.Web.HttpRequest request, System.Web.HttpResponse response) { if (CheckCookie(cookieName, request) == false) { return(false); } System.Web.HttpCookie objCookie = request.Cookies[cookieName]; objCookie.Expires = dtObsolete; try { response.AppendCookie(objCookie); } catch (Exception ex) { return(false); } return(true); }