Instantly share code, notes, and snippets.
Last active
April 9, 2017 13: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 mayurlohite/6c6799f390a32cd6dc36c5fd3fe60b75 to your computer and use it in GitHub Desktop.
A cookie wrapper class. Easily read, create, update and delete cookies in MVC. http://www.somethingtocode.com/post/a-cookie-wrapper-class-easily-read-create-update-and-delete-cookies-in-mvc
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 class CookieHelper | |
{ | |
#region Constants | |
/// <summary> | |
/// The cookie name | |
/// </summary> | |
public const string cookieName = "UserName"; | |
#endregion | |
#region Enums | |
public enum EnumCookieInterval | |
{ | |
SECOND = 0, | |
MINUTE = 1, | |
DAY = 2, | |
MONTH = 3, | |
YEAR = 4 | |
}; | |
#endregion | |
#region Utilities | |
/// <summary> | |
/// Calculates the cookie lifetime. | |
/// </summary> | |
/// <param name="duration">The duration.</param> | |
/// <param name="durationType">Type of the duration.Use enum to specify</param> | |
/// <returns>Expire date for cookie</returns> | |
private static DateTime CalculateCookieLifetime(int duration, Enum durationType) | |
{ | |
DateTime cookieExpire = DateTime.Now; | |
switch (Convert.ToInt32(durationType)) | |
{ | |
case 0: | |
cookieExpire = DateTime.Now.AddSeconds(duration); | |
break; | |
case 1: | |
cookieExpire = DateTime.Now.AddMinutes(duration); | |
break; | |
case 2: | |
cookieExpire = DateTime.Now.AddDays(duration); | |
break; | |
case 3: | |
cookieExpire = DateTime.Now.AddMonths(duration); | |
break; | |
case 4: | |
cookieExpire = DateTime.Now.AddYears(duration); | |
break; | |
default: | |
cookieExpire = DateTime.Now.AddDays(duration); | |
break; | |
} | |
return cookieExpire; | |
} | |
#endregion | |
#region Methods | |
/// <summary> | |
/// Creates the cookie. | |
/// </summary> | |
/// <param name="cookieName">Name of the cookie.</param> | |
/// <param name="cookieValue">The cookie value.</param> | |
/// <param name="durationType">Type of the duration.Use enum to specify</param> | |
/// <param name="duration">The duration.</param> | |
/// <returns></returns> | |
public static string CreateCookie(string cookieName, string cookieValue, Enum durationType, int duration) | |
{ | |
HttpCookie myCookie = new HttpCookie(cookieName); | |
// Set the cookie value. | |
myCookie.Value = cookieValue; | |
// Set the cookie expiration date. | |
myCookie.Expires = CalculateCookieLifetime(duration, durationType); | |
// Add the cookie. | |
//Response.Cookies.Add(myCookie); | |
HttpContext.Current.Response.Cookies.Add(myCookie); | |
return cookieValue; | |
} | |
/// <summary> | |
/// Reads the cookie. | |
/// </summary> | |
/// <param name="cookieName">Name of the cookie.</param> | |
/// <returns></returns> | |
public static string ReadCookie(string cookieName) | |
{ | |
string cookieValue = string.Empty; | |
HttpCookie myCookie = new HttpCookie(cookieName); | |
myCookie = HttpContext.Current.Request.Cookies[cookieName]; | |
// Read the cookie information and display it. | |
if (myCookie != null) | |
cookieValue = myCookie.Value; | |
return cookieValue; | |
} | |
/// <summary> | |
/// Updates the cookie. | |
/// </summary> | |
/// <param name="cookieName">Name of the cookie.</param> | |
/// <param name="cookieValue">The cookie value.</param> | |
/// <param name="durationType">Type of the duration.Use enum to specify</param> | |
/// <param name="duration">The duration.</param> | |
/// <returns></returns> | |
public static string UpdateCookie(string cookieName, string cookieValue, Enum durationType, int duration) | |
{ | |
HttpCookie myCookie = new HttpCookie(cookieName); | |
myCookie = HttpContext.Current.Request.Cookies[cookieName]; | |
// Set the cookie value. | |
myCookie.Value = cookieValue; | |
// Set the cookie expiration date. | |
myCookie.Expires = CalculateCookieLifetime(duration, durationType); | |
// Add the cookie. | |
//Response.Cookies.Add(myCookie); | |
HttpContext.Current.Response.Cookies.Add(myCookie); | |
return cookieValue; | |
} | |
/// <summary> | |
/// Deletes the cookie. | |
/// </summary> | |
/// <param name="cookieName">Name of the cookie.</param> | |
public static void DeleteCookie(string cookieName) | |
{ | |
HttpCookie myCookie = new HttpCookie(cookieName); | |
DateTime cookieExpire = DateTime.Now; | |
// Set the cookie expiration date. | |
myCookie.Expires = cookieExpire.AddDays(-1); | |
HttpContext.Current.Response.Cookies.Add(myCookie); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment