Skip to content

Instantly share code, notes, and snippets.

@jstemerdink
Created February 10, 2016 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstemerdink/0008608610b18d699b25 to your computer and use it in GitHub Desktop.
Save jstemerdink/0008608610b18d699b25 to your computer and use it in GitHub Desktop.
protected void Session_Start(object sender, EventArgs e)
{
this.UpdateViewedPagesSession();
this.UpdateViewedCategoriesSession();
}
protected void UpdateViewedPagesSession()
{
HttpCookie myCookie = null;
try
{
myCookie = this.Request.Cookies["EPi: ViewedPages"];
}
catch (HttpException)
{
}
if (myCookie == null)
{
return;
}
HashSet<PageReference> viewedPages = JsonConvert.DeserializeObject<HashSet<PageReference>>(myCookie.Value);
HttpContext.Current.Session["EPi:ViewedPages"] = viewedPages;
}
protected void UpdateViewedCategoriesSession()
{
try
{
foreach (string key in
this.Request.Cookies.Keys.Cast<string>().Where(key => key.Contains("EPi:VisitedCategories")))
{
HttpCookie myCookie = null;
try
{
myCookie = this.Request.Cookies[key];
}
catch (HttpException)
{
}
if (myCookie == null)
{
return;
}
if (myCookie.Value.Equals("true", StringComparison.OrdinalIgnoreCase))
{
HttpContext.Current.Session[key] = JsonConvert.DeserializeObject<bool>("true");
}
else
{
HashSet<PageReference> viewedCategories = JsonConvert.DeserializeObject<HashSet<PageReference>>(myCookie.Value);
HttpContext.Current.Session[key] = viewedCategories;
}
}
}
catch (HttpException)
{
}
catch (ArgumentNullException)
{
}
catch (InvalidCastException)
{
}
catch (ArgumentException)
{
}
}
public virtual void UpdateViewedPagesCookie()
{
HttpSessionStateBase httpSessionStateBase = null;
try
{
httpSessionStateBase = this.ControllerContext.HttpContext.Session;
}
catch (NotImplementedException)
{
}
if (httpSessionStateBase == null)
{
return;
}
HashSet<PageReference> viewedPages = null;
try
{
viewedPages = httpSessionStateBase["EPi:ViewedPages"] as HashSet<PageReference>;
}
catch (NotImplementedException)
{
}
if (viewedPages == null)
{
return;
}
string content = JsonConvert.SerializeObject(viewedPages);
HttpCookie myCookie = this.Request.Cookies["EPi: ViewedPages"];
if (myCookie == null)
{
try
{
myCookie = new HttpCookie("EPi: ViewedPages") { Value = content, Expires = DateTime.Now.AddYears(1) };
this.Response.Cookies.Add(myCookie);
}
catch (ArgumentOutOfRangeException)
{
}
catch (NotImplementedException)
{
}
return;
}
try
{
myCookie.Value = content;
myCookie.Expires = DateTime.Now.AddYears(1);
this.Response.Cookies.Set(myCookie);
}
catch (ArgumentOutOfRangeException)
{
}
catch (NotImplementedException)
{
}
}
public void UpdateVisitedCategoriesCookie()
{
HttpSessionStateBase httpSessionStateBase = null;
try
{
httpSessionStateBase = this.ControllerContext.HttpContext.Session;
}
catch (NotImplementedException)
{
}
if (httpSessionStateBase == null)
{
return;
}
HashSet<PageReference> viewdcategories = null;
try
{
foreach (string key in httpSessionStateBase.Keys.Cast<string>().Where(key => key.Contains("EPi:VisitedCategories")))
{
HttpCookie myCookie = this.Request.Cookies[key];
string content = string.Empty;
try
{
viewdcategories = httpSessionStateBase[key] as HashSet<PageReference>;
}
catch (NotImplementedException)
{
}
content = viewdcategories != null ? JsonConvert.SerializeObject(viewdcategories) : JsonConvert.SerializeObject(true);
if (myCookie == null)
{
try
{
myCookie = new HttpCookie(key) { Value = content, Expires = DateTime.Now.AddYears(1) };
this.Response.Cookies.Add(myCookie);
}
catch (ArgumentOutOfRangeException)
{
}
catch (NotImplementedException)
{
}
}
else
{
try
{
myCookie.Value = content;
myCookie.Expires = DateTime.Now.AddYears(1);
this.Response.Cookies.Set(myCookie);
}
catch (ArgumentOutOfRangeException)
{
}
catch (NotImplementedException)
{
}
}
}
}
catch (NotImplementedException)
{
}
catch (ArgumentNullException)
{
}
catch (InvalidCastException)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment