public class Cookie
{
/// <summary>
/// Writes the cookie into the response stream with the value passed. The value
/// is always the UserId.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <param name="value">The value.</param>
public static void WriteCookie(string cookieName, string value)
{
WriteCookie(cookieName, value, 1, string.Empty);
}
/// <summary>
/// Writes the cookie into the response stream with the value passed. The value
/// is always the UserId.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <param name="value">The value.</param>
/// <param name="path">The path.</param>
public static void WriteCookie(string cookieName, string value, string path)
{
WriteCookie(cookieName, value, 1, path);
}
/// <summary>
/// Writes the cookie into the response stream with the value passed. The value
/// is always the UserId.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <param name="value">The value.</param>
/// <param name="cookieTimeoutInMonths">The cookie timeout in months.</param>
/// <param name="path">The path.</param>
public static void WriteCookie(string cookieName, string value, int cookieTimeoutInMonths, string path)
{
HttpCookie cookie = new HttpCookie(cookieName, value);
if (cookieTimeoutInMonths >= 0)
cookie.Expires = DateTime.Now.AddMonths(cookieTimeoutInMonths);
if (path != string.Empty)
cookie.Path = path;
HttpContext.Current.Response.Cookies.Add(cookie);
}
/// <summary>
/// Removes the cookie by clearing it out and expiring it immediately.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
public static void Remove(string cookieName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
{
cookie.Expires = DateTime.Now.AddHours(-2);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
/// <summary>
/// Retrieves the user's Cookie. If the Cookie doesn't exist a new one is generated
/// by hashing a new GUID value and writing the Cookie into the Response.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <returns>Cookie Value</returns>
public static string Get(string cookieName)
{
return Get(cookieName, string.Empty);
}
/// <summary>
/// Retrieves the user's Cookie. If the Cookie doesn't exist a new one is generated
/// by hashing a new GUID value and writing the Cookie into the Response.
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <param name="path">The path.</param>
/// <returns>Cookie Value</returns>
public static string Get(string cookieName, string path)
{
if (DoesExist(cookieName, path))
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (path != string.Empty)
cookie.Path = path;
return (string)cookie.Value;
}
else
{
return string.Empty;
}
}
/// <summary>
/// Determines whether the cookie exists
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <returns>If the cookie exists</returns>
public static bool DoesExist(string cookieName)
{
return DoesExist(cookieName, string.Empty);
}
/// <summary>
/// Determines whether the cookie exists
/// </summary>
/// <param name="cookieName">Name of the cookie.</param>
/// <param name="path">The path.</param>
/// <returns>If the cookie exists</returns>
public static bool DoesExist(string cookieName, string path)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (path != string.Empty)
cookie.Path = path;
return (cookie != null);
}
}