C# Cookie helper utility.

using System;
using System.Configuration;
using System.Web;

namespace Helpers
{
    /// <summary>
    /// Cookie helper class
    /// </summary>
    public static class CookieHelper
    {
        private const string CookieDurationSettingName = "CookieHelper:Duration";
        private const string CookieIshttpSettingName = "CookieHelper:IsHttp";

        static CookieHelper()
        {
            Duration = GetDefaultDurationValue();
            IsHttp = GetDefaultIsHttpValue();
        }

        private static HttpContext Context
        {
            get
            {
                return HttpContext.Current;
            }
        }

        private static int Duration { get; set; }

        private static bool IsHttp { get; set; }

        public static void Set(string key, string value)
        {
            var c = new HttpCookie(key)
            {
                Value = value,
                Expires = DateTime.Now.AddDays(Duration),
                HttpOnly = IsHttp
            };

            Context.Response.Cookies.Add(c);
        }

        public static void Set(string key, string value, bool isHttp, int expireDays)
        {
            var c = new HttpCookie(key)
            {
                Value = value,
                Expires = DateTime.Now.AddDays(expireDays),
                HttpOnly = isHttp
            };

            Context.Response.Cookies.Add(c);
        }

        public static void Set(string key, string value, bool isHttp)
        {
            var c = new HttpCookie(key)
            {
                Value = value,
                Expires = DateTime.Now.AddDays(Duration),
                HttpOnly = isHttp
            };

            Context.Response.Cookies.Add(c);
        }
        public static void Set(string key, string value, int expireDays)
        {
            var c = new HttpCookie(key)
            {
                Value = value,
                Expires = DateTime.Now.AddDays(expireDays),
                HttpOnly = IsHttp
            };

            Context.Response.Cookies.Add(c);
        }

        public static string Get(string key)
        {
            string value = string.Empty;

            HttpCookie c = Context.Request.Cookies[key];

            return c != null
                   ? Context.Server.HtmlEncode(c.Value).Trim()
                   : value;
        }

        public static bool Exists(string key)
        {
            return Context.Request.Cookies[key] != null;
        }

        public static void Delete(string key)
        {
            if (Exists(key))
            {
                var c = new HttpCookie(key)
                {
                    Expires = DateTime.Now.AddDays(-10),
                    Value = null
                };

                Context.Response.Cookies.Add(c);
            }
        }

        /// <summary>
        /// Deletes all cookies.
        /// </summary>
        /// <param name="deleteServerCookies">True to delete server cookies.
        /// The default is false.</param>
        public static void DeleteAll(bool deleteServerCookies = false)
        {
            for (int i = 0; i <= Context.Request.Cookies.Count - 1; i++)
            {
                if (Context.Request.Cookies[i] != null)
                {
                    Delete(Context.Request.Cookies[i].Name);
                }
            }

            if (deleteServerCookies)
            {
                Context.Request.Cookies.Clear();
            }
        }

        private static int GetDefaultDurationValue()
        {
            //default
            int duration = 30;

            string setting = ConfigurationManager.AppSettings[CookieDurationSettingName];
            if (!string.IsNullOrEmpty(setting))
            {
                int.TryParse(setting, out duration);
            }

            return duration;
        }

        private static bool GetDefaultIsHttpValue()
        {
            //default
            bool isHttp = true;

            string setting = ConfigurationManager.AppSettings[CookieIshttpSettingName];
            if (!string.IsNullOrEmpty(setting))
            {
                bool.TryParse(setting, out isHttp);
            }

            return isHttp;
        }
    }
}