Set cookie to client
public class CookieStore
{
public static void SetCookie(string key, string value)
{
HttpCookie encodedCookie = new HttpCookie(key,EncryptString.Encrypt(value));
if (HttpContext.Current.Request.Cookies[key] != null)
{
var cookieOld = HttpContext.Current.Request.Cookies[key];
cookieOld.Expires = DateTime.Now.AddDays(15);
cookieOld.Value = encodedCookie.Value;
HttpContext.Current.Response.Cookies.Add(cookieOld);
}
else
{
encodedCookie.Expires = DateTime.Now.AddDays(15);
HttpContext.Current.Response.Cookies.Add(encodedCookie);
}
}
public static void SetCookieNotEnc(string key, string value)
{
HttpCookie encodedCookie = new HttpCookie(key, value);
if (HttpContext.Current.Request.Cookies[key] != null)
{
var cookieOld = HttpContext.Current.Request.Cookies[key];
cookieOld.Expires = DateTime.Now.AddDays(15);
cookieOld.Value = encodedCookie.Value;
HttpContext.Current.Response.Cookies.Add(cookieOld);
}
else
{
encodedCookie.Expires = DateTime.Now.AddDays(15);
HttpContext.Current.Response.Cookies.Add(encodedCookie);
}
}
public static string GetCookie(string key)
{
string value = string.Empty;
HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
if (cookie != null)
{
value = cookie.Value;
}
return value;
}
}