Instantly share code, notes, and snippets.
Created
June 11, 2024 21:51
-
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 electricessence/9beda7aaad425a097f2b20914a509421 to your computer and use it in GitHub Desktop.
SmartCookie.cs: Legacy WebForms Cookie Management
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Web; | |
using System.Collections.Specialized; | |
using System.Diagnostics.Contracts; | |
using System.Threading; | |
using Utilities.Threading; | |
namespace Utilities.Web | |
{ | |
public static class Cookie | |
{ | |
internal static void Kill(this HttpCookie c) | |
{ | |
Contract.Requires(c != null); | |
var context = HttpContext.Current; | |
Contract.Assume(context != null); | |
context.Request.Cookies.Remove(c.Name); | |
c.Value = null; | |
c.Expires = DateTime.MinValue; | |
context.Response.AppendCookie(c); | |
} | |
public static string ValueFrom(string name) | |
{ | |
return new SmartCookie(name).Value; | |
} | |
public static NameValueCollection ValuesFrom(string name) | |
{ | |
return new SmartCookie(name).Values; | |
} | |
public static void Delete(string name) { new SmartCookie(name).Delete(); } | |
} | |
public sealed class SmartCookie | |
{ | |
public SmartCookie(String name) { _name = name; } | |
static HttpRequest Request | |
{ | |
get { | |
Contract.Assume(HttpContext.Current != null); | |
return HttpContext.Current.Request; | |
} | |
} | |
static HttpResponse Response | |
{ | |
get { | |
Contract.Assume(HttpContext.Current != null); | |
return HttpContext.Current.Response; | |
} | |
} | |
private HttpCookie _cookie; | |
public HttpCookie Cookie | |
{ | |
get | |
{ | |
Contract.Ensures(Contract.Result<HttpCookie>() != null); | |
var r = LazyInitializer.EnsureInitialized(ref _cookie, () => | |
{ | |
if (IsInResponse) // Use cookie being written to. | |
return Response.Cookies[_name]; | |
else if (Request.Cookies[_name] != null) // Use cookie from request. | |
return Request.Cookies[_name]; | |
else return new HttpCookie(_name); // Create new cookie(). | |
}); | |
Contract.Assume(r != null); | |
return r; | |
} | |
} | |
public Boolean IsInResponse | |
{ | |
get { return Response.Cookies.AllKeys.Any(n=>n.Equals(_name,StringComparison.CurrentCultureIgnoreCase)); } | |
} | |
public Boolean IsInRequest | |
{ | |
get { return Request.Cookies.AllKeys.Any(n=>n.Equals(_name,StringComparison.CurrentCultureIgnoreCase)); } | |
} | |
public Boolean IsNew | |
{ | |
get { return !IsInRequest && !IsInResponse; } | |
} | |
public Boolean IsPersisting | |
{ | |
get { return Expires > DateTime.Now; } | |
} | |
public Boolean IsDead | |
{ | |
get { return IsNew || !IsPersisting && String.IsNullOrWhiteSpace(Value); } | |
} | |
private String _name; | |
public String Name | |
{ | |
get { return _name; } | |
set | |
{ | |
HttpCookie c = Cookie; | |
Delete(); | |
c.Name = value; | |
Response.AppendCookie(c); | |
} | |
} | |
void Update() | |
{ | |
Response.Cookies.Set(Cookie); | |
} | |
public Boolean HasKeys | |
{ | |
get { return Cookie.HasKeys; } | |
} | |
public const int MAXIMUMSIZE = 4096; | |
public String Value | |
{ | |
get { return Cookie.Value; } | |
set | |
{ | |
Contract.Requires(value == null || value.Length < MAXIMUMSIZE); | |
Cookie.Value = value; | |
Update(); | |
} | |
} | |
internal NameValueCollection Values | |
{ | |
get | |
{ | |
Contract.Ensures(Contract.Result<NameValueCollection>() != null); | |
return Cookie.Values; | |
} | |
} | |
public String this[string key] | |
{ | |
get { | |
return Values[key]; | |
} | |
set | |
{ | |
Values[key] = value; | |
Contract.Assume(Value==null || Value.Length < MAXIMUMSIZE); | |
Update(); | |
} | |
} | |
public String Path | |
{ | |
get { return Cookie.Path; } | |
set | |
{ | |
Cookie.Path = value; | |
Update(); | |
} | |
} | |
public DateTime Expires | |
{ | |
get { return Cookie.Expires; } | |
set | |
{ | |
Cookie.Expires = value; | |
Update(); | |
} | |
} | |
public TimeSpan LifeSpan | |
{ | |
get | |
{ | |
var expires = Expires; | |
if (expires <= DateTime.Now) return TimeSpan.Zero; | |
return Expires - DateTime.Now; | |
} | |
set | |
{ | |
Cookie.Expires = DateTime.Now.Add(value); | |
Update(); | |
} | |
} | |
public String Domain | |
{ | |
set | |
{ | |
Cookie.Domain = value; | |
Update(); | |
} | |
get { return Cookie.Domain; } | |
} | |
public Boolean Secure | |
{ | |
set | |
{ | |
Cookie.Secure = value; | |
Update(); | |
} | |
get { return Cookie.Secure; } | |
} | |
public void Delete() | |
{ | |
if (!IsNew) | |
{ | |
if (IsInRequest) | |
Cookie.Kill(); | |
else | |
{ | |
var c = _cookie; | |
if (c != null) | |
{ | |
c.Value = null; | |
c.Expires = DateTime.MinValue; | |
} | |
Response.Cookies.Remove(_name); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment