Skip to content

Instantly share code, notes, and snippets.

@Migaroez
Created February 9, 2017 14:11
Show Gist options
  • Save Migaroez/4fd33bdca2e189eec2dd534c0838d323 to your computer and use it in GitHub Desktop.
Save Migaroez/4fd33bdca2e189eec2dd534c0838d323 to your computer and use it in GitHub Desktop.
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;
}
}
}
@Migaroez
Copy link
Author

Migaroez commented Feb 9, 2017

Usage controller: HttpContext.Current.Request.RequestContext.HttpContext.
Usage razor: Html.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment