Created
February 10, 2016 16:17
-
-
Save jstemerdink/0008608610b18d699b25 to your computer and use it in GitHub Desktop.
This file contains 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
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) | |
{ | |
} | |
} |
This file contains 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 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