/ Published in: C#
You should include these Cookie helpers in your base page class. Enjoy!
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#region - Cookies - /// <summary> /// Reads the cookie. /// </summary> /// <param name="cookieName">Name of the cookie.</param> /// <returns></returns> public string ReadCookie(string cookieName) { HttpCookie httpCookie = Request.Cookies[cookieName]; return httpCookie != null ? Server.HtmlEncode(httpCookie.Value).Trim() : string.Empty; } /// <summary> /// Writes a cookie and auto sets the expire date to as current day plus one. /// </summary> /// <param name="cookieName">Name of the cookie.</param> /// <param name="cookieValue">The cookie value.</param> /// <param name="isHttpCookie">if set to <c>true</c> [is HTTP cookie].</param> public void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie) { {Value = cookieValue, Expires = DateTime.Now.AddDays(1), HttpOnly = isHttpCookie}; Response.Cookies.Add(aCookie); } /// <summary> /// Writes a cookie. /// </summary> /// <param name="cookieName">Name of the cookie.</param> /// <param name="cookieValue">The cookie value.</param> /// <param name="isHttpCookie">if set to <c>true</c> [is HTTP cookie].</param> /// <param name="cookieExpireDate">The cookie expire date.</param> public void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie, DateTime cookieExpireDate) { {Value = cookieValue, Expires = cookieExpireDate, HttpOnly = isHttpCookie}; Response.Cookies.Add(aCookie); } /// <summary> /// Deletes a single cookie. /// </summary> /// <param name="cookieName">Name of the cookie.</param> public void DeleteCookie(string cookieName) { Response.Cookies.Add(aCookie); } /// <summary> /// Deletes all the cookies available to the application. /// </summary> /// <remarks>The technique creates a new cookie with the same name as the cookie to be deleted, but to set the cookie's expiration to a date earlier than today.</remarks> public void DeleteAllCookies() { for (int i = 0; i <= Request.Cookies.Count - 1; i++) { {Expires = DateTime.Now.AddDays(-1)}); } } #endregion