Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
 /// <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);
 }