using System; using System.Collections.Specialized; using System.Web; public static class CookieHelper { public static void ClearCartCookie(Guid storeGuid) { if (storeGuid != Guid.Empty) { SetPersistentCookie(GetCartKey(storeGuid), string.Empty); } } public static bool CookieExists(string cookieName) { if (HttpContext.Current == null) { return false; } if (string.IsNullOrEmpty(cookieName)) { return false; } return (HttpContext.Current.Request.Cookies[cookieName] != null); } public static string DecryptAndVerifyCookie(HttpCookie cookie, NameValueCollection serverVariables) { string[] strArray; if (cookie == null) { return null; } if (!CryptoHelper.DecryptAndVerifyData(cookie.Value, out strArray)) { return null; } if ((strArray.Length == 3) && (DateTime.Parse(strArray[2]) < DateTime.Now)) { return null; } if (strArray[1] != serverVariables["REMOTE_ADDR"]) { return null; } return strArray[0]; } public static void ExpireCookie(string cookieName) { if (!string.IsNullOrEmpty(cookieName) && (HttpContext.Current != null)) { HttpCookie cookie = new HttpCookie(cookieName, string.Empty); cookie.HttpOnly = true; cookie.Expires = DateTime.Now.AddYears(-5); HttpContext.Current.Response.Cookies.Add(cookie); } } public static string GetCartCookie(Guid storeGuid) { if (storeGuid == Guid.Empty) { return string.Empty; } return GetCookieValue(GetCartKey(storeGuid)); } public static string GetCartKey(Guid storeGuid) { return ("cart" + storeGuid.ToString()); } public static string GetCookieValue(string cookieName) { if (HttpContext.Current == null) { return string.Empty; } if (string.IsNullOrEmpty(cookieName)) { return string.Empty; } if (HttpContext.Current.Request.Cookies[cookieName] == null) { return string.Empty; } return HttpContext.Current.Request.Cookies.Get(cookieName).Value; } public static string GetSecureCookieValue(string cookieName) { if (HttpContext.Current == null) { return string.Empty; } if (string.IsNullOrEmpty(cookieName)) { return string.Empty; } HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookieName); if (cookie == null) { return string.Empty; } return DecryptAndVerifyCookie(cookie, HttpContext.Current.Request.ServerVariables).ToString(); } public static void SetCartCookie(Guid storeGuid, Guid cartGuid) { if ((storeGuid != Guid.Empty) && (cartGuid != Guid.Empty)) { SetPersistentCookie("cart" + storeGuid.ToString(), cartGuid.ToString()); } } public static void SetCookie(string cookieName, string cookieValue) { if ((!string.IsNullOrEmpty(cookieName) && !string.IsNullOrEmpty(cookieValue)) && (HttpContext.Current != null)) { HttpCookie cookie = new HttpCookie(cookieName, cookieValue); cookie.HttpOnly = true; HttpContext.Current.Response.Cookies.Add(cookie); } } public static void SetCookie(string cookieName, string cookieValue, bool persistent) { if ((!string.IsNullOrEmpty(cookieName) && !string.IsNullOrEmpty(cookieValue)) && (HttpContext.Current != null)) { if (persistent) { SetPersistentCookie(cookieName, cookieValue); } else { SetCookie(cookieName, cookieValue); } } } public static void SetPersistentCookie(string cookieName, string cookieValue) { if ((!string.IsNullOrEmpty(cookieName) && !string.IsNullOrEmpty(cookieValue)) && (HttpContext.Current != null)) { HttpCookie cookie = new HttpCookie(cookieName, cookieValue); cookie.HttpOnly = true; cookie.Expires = DateTime.Now.AddYears(1); HttpContext.Current.Response.Cookies.Add(cookie); } } public static void SetSecureCookie(string cookieName, string cookieValue) { if ((!string.IsNullOrEmpty(cookieName) && !string.IsNullOrEmpty(cookieValue)) && (HttpContext.Current != null)) { HttpCookie cookie = new HttpCookie(cookieName, cookieValue); cookie.HttpOnly = true; SignAndSecureCookie(cookie, HttpContext.Current.Request.ServerVariables); HttpContext.Current.Response.Cookies.Add(cookie); } } public static void SignAndSecureCookie(HttpCookie cookie, NameValueCollection serverVariables) { if (cookie.HasKeys) { throw new Exception("Does not support cookies with sub keys"); } if (cookie.Expires != DateTime.MinValue) { cookie.Value = CryptoHelper.SignAndSecureData(new string[] { cookie.Value, serverVariables["REMOTE_ADDR"], cookie.Expires.ToString() }); } else { cookie.Value = CryptoHelper.SignAndSecureData(new string[] { cookie.Value, serverVariables["REMOTE_ADDR"] }); } } public static bool UserHasCartCookie(Guid storeGuid) { if (storeGuid == Guid.Empty) { return false; } return CookieExists("cart" + storeGuid.ToString()); } }
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.