Instantly share code, notes, and snippets.
Created
May 29, 2012 19:44
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save mikeobrien/2830278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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