using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace Neo.Afx.Common { public static class HttpCookieHelper { /// /// Reads the cookie. /// /// Name of the cookie. /// public static string ReadCookie(string cookieName) { HttpCookie httpCookie = HttpContext.Current.Request.Cookies[cookieName]; return httpCookie != null ? HttpContext.Current.Server.HtmlEncode(httpCookie.Value).Trim() : string.Empty; } /// /// Writes a cookie and auto sets the expire date to as current day plus one. /// /// Name of the cookie. /// The cookie value. /// if set to true [is HTTP cookie]. public static void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie) { var aCookie = new HttpCookie(cookieName) {Value = cookieValue, Expires = DateTime.Now.AddDays(1), HttpOnly = isHttpCookie}; HttpContext.Current.Response.Cookies.Add(aCookie); } /// /// Writes a cookie. /// /// Name of the cookie. /// The cookie value. /// if set to true [is HTTP cookie]. /// The cookie expire date. public static void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie, DateTime cookieExpireDate) { var aCookie = new HttpCookie(cookieName) {Value = cookieValue, Expires = cookieExpireDate, HttpOnly = isHttpCookie}; HttpContext.Current.Response.Cookies.Add(aCookie); } /// /// Deletes a single cookie. /// /// Name of the cookie. public static void DeleteCookie(string cookieName) { var aCookie = new HttpCookie(cookieName) {Expires = DateTime.Now.AddDays(-1)}; HttpContext.Current.Response.Cookies.Add(aCookie); } } }