#region 保存用户名的Cookie /// <summary> /// 设置Cookie /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expires"></param> /// <returns></returns> public HttpCookie SetCookie(string cookieName, string key, string value, DateTime? expires) { HttpResponse response = System.Web.HttpContext.Current.Response; if (response != null) { HttpCookie cookie = response.Cookies[cookieName]; if (cookie != null) { if (!string.IsNullOrEmpty(key) && cookie.HasKeys) cookie.Values.Set(key, value); else if (!string.IsNullOrEmpty(value)) cookie.Value = value; if (expires != null) cookie.Expires = expires.Value; response.SetCookie(cookie); } return cookie; } else { return null; } } /// <summary> /// 获得Cookie /// </summary> /// <param name="cookieName"></param> /// <returns></returns> public HttpCookie GetCookie(string cookieName) { bool isReusable = HttpContext.CurrentHandler.IsReusable; HttpRequest request = System.Web.HttpContext.Current.Request; if (request != null) return request.Cookies[cookieName]; return null; } /// <summary> /// 删除Cookies /// </summary> /// <param name="cookieName"></param> /// <param name="key"></param> public static void RemoveCookie(string cookieName, string key) { HttpResponse response = System.Web.HttpContext.Current.Response; if (response != null) { HttpCookie cookie = response.Cookies[cookieName]; if (cookie != null) { if (!string.IsNullOrEmpty(key)) cookie.Values.Remove(key); else response.Cookies.Remove(cookieName); } } } #endregion