Return to Snippet

Revision: 34812
at July 1, 2012 04:51 by mecha


Updated Code
#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)
        {
            var aCookie = new HttpCookie(cookieName)
                              {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)
        {
            var aCookie = new HttpCookie(cookieName)
                              {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)
        {
            var aCookie = new HttpCookie(cookieName) {Expires = DateTime.Now.AddDays(-1)};
            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++)
            {
                Response.Cookies.Add(new HttpCookie(Request.Cookies[i].Name)
                                         {Expires = DateTime.Now.AddDays(-1)});
            }
        }

        #endregion

Revision: 34811
at June 1, 2011 14:51 by mecha


Updated Code
#region "- Cookies -"

/// <summary>
/// Reads the cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <returns></returns>
protected string ReadCookie(string cookieName) {
    return (this.Request.Cookies[cookieName] != null) ? this.Server.HtmlEncode(this.Request.Cookies[cookieName].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>
protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie) {
    HttpCookie aCookie = new HttpCookie(cookieName) {Value = cookieValue, Expires = DateTime.Now.AddDays(1), HttpOnly = isHttpCookie};
    this.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>
protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie, DateTime cookieExpireDate) {
    HttpCookie aCookie = new HttpCookie(cookieName) {Value = cookieValue, Expires = cookieExpireDate, HttpOnly = isHttpCookie};
    this.Response.Cookies.Add(aCookie);
}

/// <summary>
/// Deletes a single cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
protected void DeleteCookie(string cookieName) {
    HttpCookie aCookie = new HttpCookie(cookieName) {Expires = System.DateTime.Now.AddDays(-1)};
    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>
protected void DeleteAllCookies() {
    for (var i = 0; i <= this.Request.Cookies.Count - 1; i++) {
        this.Response.Cookies.Add(new HttpCookie(this.Request.Cookies[i].Name) {Expires = DateTime.Now.AddDays(-1)});
    }
}

#endregion

Revision: 34810
at June 1, 2011 01:06 by mecha


Updated Code
#region "- Cookies -"

        /// <summary>
        /// Reads the cookie.
        /// </summary>
        /// <param name="cookieName">Name of the cookie.</param>
        /// <returns></returns>
        protected string ReadCookie(string cookieName) {
            return (this.Request.Cookies[cookieName] != null) ? this.Server.HtmlEncode(this.Request.Cookies[cookieName].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>
        protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie) {
            HttpCookie aCookie = new HttpCookie(cookieName) {Value = cookieValue, Expires = DateTime.Now.AddDays(1), HttpOnly = isHttpCookie};
            this.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>
        protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie, DateTime cookieExpireDate) {
            HttpCookie aCookie = new HttpCookie(cookieName) {Value = cookieValue, Expires = cookieExpireDate, HttpOnly = isHttpCookie};
            this.Response.Cookies.Add(aCookie);
        }

        /// <summary>
        /// Deletes a single cookie.
        /// </summary>
        /// <param name="cookieName">Name of the cookie.</param>
        protected void DeleteCookie(string cookieName) {
            HttpCookie aCookie = new HttpCookie(cookieName) {Expires = System.DateTime.Now.AddDays(-1)};
            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>
        protected void DeleteAllCookies() {
            for (var i = 0; i <= this.Request.Cookies.Count - 1; i++) {
                this.Response.Cookies.Add(new HttpCookie(this.Request.Cookies[i].Name) {Expires = DateTime.Now.AddDays(-1)});
            }
        }

        #endregion

Revision: 34809
at October 28, 2010 08:58 by mecha


Updated Code
#region "- Cookies -"

/// <summary>
/// Reads the cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <returns></returns>
protected string ReadCookie(string cookieName) {
	return (this.Request.Cookies(cookieName) != null) ? this.Server.HtmlEncode(this.Request.Cookies(cookieName).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>
protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie) {
	HttpCookie aCookie = new HttpCookie(cookieName) { Value = cookieValue, Expires = DateTime.Now.AddDays(1), HttpOnly = isHttpCookie };
	this.Response.Cookies.Add(aCookie);
}

/// <summary>
/// Writes a cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <param name="cookieValue">The cookie value.</param>
protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie, DateTime cookieExpireDate) {
	HttpCookie aCookie = new HttpCookie(cookieName) { Value = cookieValue, Expires = cookieExpireDate, HttpOnly = isHttpCookie };
	this.Response.Cookies.Add(aCookie);
}

/// <summary>
/// Deletes a single cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
protected void DeleteCookie(string cookieName) {
	HttpCookie aCookie = new HttpCookie(cookieName) { Expires = System.DateTime.Now.AddDays(-1) };
	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>
protected void DeleteAllCookies() {
	int i = 0;
	for (i = 0; i <= this.Request.Cookies.Count - 1; i++) {
		this.Response.Cookies.Add(new HttpCookie(this.Request.Cookies(i).Name) { Expires = DateTime.Now.AddDays(-1) });
	}
}

#endregion

Revision: 34808
at October 28, 2010 08:53 by mecha


Updated Code
#region "- Cookies -"

/// <summary>
/// Reads the cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <returns></returns>
protected string ReadCookie(string cookieName) {
	return (this.Request.Cookies(cookieName) != null) ? this.Server.HtmlEncode(this.Request.Cookies(cookieName).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>
protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie) {
	HttpCookie aCookie = new HttpCookie(cookieName) {
		Value = cookieValue,
		Expires = DateTime.Now.AddDays(1),
		HttpOnly = isHttpCookie
	};
	this.Response.Cookies.Add(aCookie);
}

/// <summary>
/// Writes a cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <param name="cookieValue">The cookie value.</param>
protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie, DateTime cookieExpireDate) {
	HttpCookie aCookie = new HttpCookie(cookieName) {
		Value = cookieValue,
		Expires = cookieExpireDate,
		HttpOnly = isHttpCookie
	};
	this.Response.Cookies.Add(aCookie);
}

/// <summary>
/// Deletes a single cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
protected void DeleteCookie(string cookieName) {
	HttpCookie aCookie = new HttpCookie(cookieName) { Expires = System.DateTime.Now.AddDays(-1) };
	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>
protected void DeleteAllCookies() {
	int i = 0;
	for (i = 0; i <= this.Request.Cookies.Count - 1; i++) {
		this.Response.Cookies.Add(new HttpCookie(this.Request.Cookies(i).Name) { Expires = DateTime.Now.AddDays(-1) });
	}
}

#endregion

Revision: 34807
at October 28, 2010 08:52 by mecha


Initial Code
#region "- Cookies -"

/// <summary>
/// Reads the cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <returns></returns>
protected string ReadCookie(string cookieName) {
	return (this.Request.Cookies(cookieName) != null) ? this.Server.HtmlEncode(this.Request.Cookies(cookieName).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>
protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie) {
	HttpCookie aCookie = new HttpCookie(cookieName) {
		Value = cookieValue,
		Expires = DateTime.Now.AddDays(1),
		HttpOnly = isHttpCookie
	};
	this.Response.Cookies.Add(aCookie);
}

/// <summary>
/// Writes a cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <param name="cookieValue">The cookie value.</param>
protected void WriteCookie(string cookieName, string cookieValue, bool isHttpCookie, DateTime cookieExpireDate) {
	HttpCookie aCookie = new HttpCookie(cookieName) {
		Value = cookieValue,
		Expires = cookieExpireDate,
		HttpOnly = isHttpCookie
	};
	this.Response.Cookies.Add(aCookie);
}

/// <summary>
/// Deletes a single cookie.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
protected void DeleteCookie(string cookieName) {
	HttpCookie aCookie = new HttpCookie(cookieName) { Expires = System.DateTime.Now.AddDays(-1) };
	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>
protected void DeleteAllCookies() {
	int i = 0;
	for (i = 0; i <= this.Request.Cookies.Count - 1; i++) {
		this.Response.Cookies.Add(new HttpCookie(this.Request.Cookies(i).Name) { Expires = DateTime.Now.AddDays(-1) });
	}
}

#endregion

Initial URL

                                

Initial Description
You should include these Cookie helpers in your base page class. Enjoy!

Initial Title
Server-side Cookie Helper Methods

Initial Tags
aspnet, c#

Initial Language
C#