Instantly share code, notes, and snippets.
Created
February 9, 2017 14:11
-
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 Migaroez/4fd33bdca2e189eec2dd534c0838d323 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
using System.Collections.Generic; | |
// ReSharper disable once CheckNamespace | |
namespace System.Web.Mvc | |
{ | |
public static class CookieExtensions | |
{ | |
/// <summary> | |
/// Creates a cookie if it doesnt exists and gets the item if it exists | |
/// </summary> | |
/// <param name="html"></param> | |
/// <param name="cookieName"></param> | |
/// <param name="itemkey"></param> | |
/// <returns></returns> | |
public static string GetCookieValue(this HtmlHelper html, string cookieName, string itemkey) | |
{ | |
return GetCookie(html.ViewContext.HttpContext,cookieName).Values[itemkey]; | |
} | |
/// <summary> | |
/// Creates a cookie if it doesnt exists and sets the item | |
/// </summary> | |
/// <param name="context"></param> | |
/// <param name="cookieName"></param> | |
/// <param name="itemkey"></param> | |
/// <param name="value"></param> | |
public static void SetCookieValue(this HttpContextBase context, string cookieName, string itemkey, string value) | |
{ | |
GetCookie(context, cookieName).Values[itemkey] = value; | |
} | |
public static void RemoveCookieValue(this HttpContextBase context, string cookieName, string itemkey) | |
{ | |
GetCookie(context, cookieName).Values.Remove(itemkey); | |
} | |
/// <summary> | |
/// Creates a cookie if it doesnt exists and gets the item if it exists | |
/// </summary> | |
/// <param name="context"></param> | |
/// <param name="cookieName"></param> | |
/// <param name="itemkey"></param> | |
/// <returns></returns> | |
public static string GetCookieValue(this HttpContextBase context, string cookieName, string itemkey) | |
{ | |
return GetCookie(context, cookieName).Values[itemkey]; | |
} | |
/// <summary> | |
/// Creates a cookie if it doesnt exists and sets the item | |
/// </summary> | |
/// <param name="html"></param> | |
/// <param name="cookieName"></param> | |
/// <param name="itemkey"></param> | |
/// <param name="value"></param> | |
public static void SetCookieValue(this HtmlHelper html, string cookieName, string itemkey, string value) | |
{ | |
GetCookie(html.ViewContext.HttpContext, cookieName).Values[itemkey] = value; | |
} | |
public static void RemoveCookieValue(this HtmlHelper html, string cookieName, string itemkey) | |
{ | |
GetCookie(html.ViewContext.HttpContext, cookieName).Values.Remove(itemkey); | |
} | |
private static HttpCookie GetCookie(HttpContextBase context, string cookieName) | |
{ | |
//see if we have the cookie in the cookielist | |
var cookie = context.Request.Cookies.Get(cookieName); | |
if (cookie != null) | |
{ | |
return cookie; | |
} | |
cookie = new HttpCookie(cookieName); | |
context.Request.Cookies.Set(cookie); | |
return cookie; | |
} | |
/// <summary> | |
/// Saves any changed or new cookies | |
/// </summary> | |
/// <param name="html"></param> | |
/// <param name="expireDateTime">Default will be 1 year from now</param> | |
public static void SaveCookies(this HtmlHelper html, DateTime? expireDateTime = null) | |
{ | |
html.ViewContext.HttpContext.SaveCookies(expireDateTime); | |
} | |
/// <summary> | |
/// Save a cookie if it was changed, or newly created | |
/// </summary> | |
/// <param name="html"></param> | |
/// <param name="cookieName"></param> | |
/// <param name="expireDateTime">Default will be 1 year from now</param> | |
public static void SaveCookie(this HtmlHelper html, string cookieName, DateTime? expireDateTime = null) | |
{ | |
html.ViewContext.HttpContext.SaveCookie(cookieName,expireDateTime); | |
} | |
/// <summary> | |
/// Saves any changed or new cookies | |
/// </summary> | |
/// <param name="context"></param> | |
/// <param name="expireDateTime">Default will be 1 year from now</param> | |
public static void SaveCookies(this HttpContextBase context, DateTime? expireDateTime = null) | |
{ | |
// adding a cookie to the response, also adds it again to the request this is why we use a buffer | |
List<HttpCookie> cookielist = new List<HttpCookie>(); | |
// add all cookies to the buffer | |
for (int i = 0; i < context.Request.Cookies.Count; i++) | |
{ | |
var cookie = context.Request.Cookies[i]; | |
cookie.Expires = expireDateTime ?? DateTime.Now.AddYears(1); | |
cookielist.Add(cookie); | |
} | |
// remove from request and add to response (which would double add it to the request) | |
foreach (var cookie in cookielist) | |
{ | |
context.Request.Cookies.Remove(cookie.Name); | |
//context.Response.Cookies.Set(cookie); | |
SetResponseCookie(context, cookie, expireDateTime); | |
} | |
} | |
/// <summary> | |
/// Save a cookie if it was changed, or newly created | |
/// </summary> | |
/// <param name="context"></param> | |
/// <param name="cookieName"></param> | |
/// <param name="expireDateTime">Default will be 1 year from now</param> | |
public static void SaveCookie(this HttpContextBase context, string cookieName, DateTime? expireDateTime = null) | |
{ | |
// adding a cookie to the response, also adds it again to the request this is why we use a buffer | |
var cookie = context.Request.Cookies.Get(cookieName); | |
if (cookie == null) | |
{ | |
return; | |
} | |
cookie.Expires = expireDateTime ?? DateTime.Now.AddYears(1); | |
context.Request.Cookies.Remove(cookie.Name); | |
SetResponseCookie(context, cookie, expireDateTime); | |
} | |
private static void SetResponseCookie(HttpContextBase context, HttpCookie cookie, DateTime? expireDateTime = null) | |
{ | |
if (context == null || cookie == null || string.IsNullOrWhiteSpace(cookie.Name)) | |
{ | |
return; | |
} | |
context.Response.Cookies[cookie.Name].Name = cookie.Name; | |
context.Response.Cookies[cookie.Name].Value = cookie.Value; | |
context.Response.Cookies[cookie.Name].Expires = expireDateTime ?? DateTime.Now.AddYears(1); | |
context.Response.Cookies[cookie.Name].Secure = cookie.Secure; | |
context.Response.Cookies[cookie.Name].Domain = cookie.Domain; | |
context.Response.Cookies[cookie.Name].Shareable = cookie.Shareable; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage controller: HttpContext.Current.Request.RequestContext.HttpContext.
Usage razor: Html.