Skip to content

Instantly share code, notes, and snippets.

@mikeobrien
Created May 29, 2012 19:44
Show Gist options
  • Save mikeobrien/2830278 to your computer and use it in GitHub Desktop.
Save mikeobrien/2830278 to your computer and use it in GitHub Desktop.
public class Cookies : ICookies
{
private readonly Lazy<HttpCookieCollection> _requestCookies = new Lazy<HttpCookieCollection>(
() => HttpContext.Current != null ? HttpContext.Current.Request.Cookies : null);
private readonly Lazy<HttpCookieCollection> _responseCookies = new Lazy<HttpCookieCollection>(
() => HttpContext.Current != null ? HttpContext.Current.Response.Cookies : null);
public void Add(string name, string value, bool secure = false, bool httpOnly = false)
{
_responseCookies.Value.Set(new HttpCookie(name, value) { Secure = true, HttpOnly = true });
}
public void Add(string name, string value, TimeSpan expires, bool secure = false, bool httpOnly = false)
{
_responseCookies.Value.Set(new HttpCookie(name, value) { Expires = DateTime.Now + expires, Secure = true, HttpOnly = true });
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return KeyAndValues.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<string, string> item)
{
Add(item.Key, item.Value);
}
public void Clear()
{
_responseCookies.Value.Clear();
}
public bool Contains(KeyValuePair<string, string> item)
{
return ContainsKey(item.Key);
}
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
{
Array.Copy(KeyAndValues.ToArray(), array, arrayIndex);
}
public bool Remove(KeyValuePair<string, string> item)
{
return Remove(item.Key);
}
public int Count
{
get { return _requestCookies.Value.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool ContainsKey(string key)
{
return Keys.Contains(key);
}
public void Add(string key, string value)
{
_responseCookies.Value.Set(new HttpCookie(key, value));
}
public bool Remove(string key)
{
var exists = ContainsKey(key);
if (exists) _responseCookies.Value.Remove(key);
return exists;
}
public bool TryGetValue(string key, out string value)
{
var exists = ContainsKey(key);
value = exists ? this[key] : null;
return exists;
}
public string this[string key]
{
get { return ContainsKey(key) ? _requestCookies.Value.Get(key).Value : null; }
set { Add(key, value); }
}
public ICollection<string> Keys
{
get { return _requestCookies.Value.Keys.Cast<string>().ToList(); }
}
public ICollection<string> Values
{
get { return Keys.Select(x => this[x]).ToList(); }
}
public IEnumerable<KeyValuePair<string, string>> KeyAndValues
{
get { return Keys.ToDictionary(x => x, x => this[x]); }
}
public override string ToString()
{
return this.ToUrlEncodedString();
}
}
public interface ICookies : IDictionary<string, string>
{
void Add(string name, string value, bool secure = false, bool httpOnly = false);
void Add(string name, string value, TimeSpan expires, bool secure = false, bool httpOnly = false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment