Skip to content

Instantly share code, notes, and snippets.

@abhilash0001
Created August 6, 2014 20:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abhilash0001/5a7cfc3413b3e1b0c2d6 to your computer and use it in GitHub Desktop.
Save abhilash0001/5a7cfc3413b3e1b0c2d6 to your computer and use it in GitHub Desktop.
Cookie Helper - C#
using System;
using System.Configuration;
using System.Web;
namespace Custom.Helpers.Common
{
public static class Cookie
{
private const string CookieSetting = "Cookie.Duration";
private const string CookieIsHttp = "Cookie.IsHttp";
private static HttpContext _context {get { return HttpContext.Current; }}
private static int _cookieDuration { get; set; }
private static bool _cookieIsHttp { get; set; }
static Cookie()
{
_cookieDuration = GetCookieDuration();
_cookieIsHttp = GetCookieType();
}
public static void Set(string key, string value)
{
var c = new HttpCookie(key)
{
Value = value,
Expires = DateTime.Now.AddDays(_cookieDuration),
HttpOnly = _cookieIsHttp
};
_context.Response.Cookies.Add(c);
}
public static string Get(string key)
{
var value = string.Empty;
var 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(-1)};
_context.Response.Cookies.Add(c);
}
}
public static void DeleteAll()
{
for (int i = 0; i <= _context.Request.Cookies.Count - 1; i++)
{
if (_context.Request.Cookies[i] != null)
Delete(_context.Request.Cookies[i].Name);
}
}
private static int GetCookieDuration()
{
//default
int duration = 360;
var setting = ConfigurationManager.AppSettings[CookieSetting];
if (!string.IsNullOrEmpty(setting))
int.TryParse(setting, out duration);
return duration;
}
private static bool GetCookieType()
{
//default
var isHttp = true;
var setting = ConfigurationManager.AppSettings[CookieIsHttp];
if (!string.IsNullOrEmpty(setting))
bool.TryParse(setting, out isHttp);
return isHttp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment