简单建立Cookie:
public enum CookieKey { UserLogin } public class CookieStorage { private const int LOGINEXPIRESTIME = 24 * 60; public static object SetCookie(CookieKey key, string value) { HttpCookie cookie = HttpContext.Current.Request.Cookies[key.ToString()]; if (cookie == null) { cookie = new HttpCookie(key.ToString()); string result = HttpUtility.UrlEncode(value); cookie.Value = result; cookie.Expires = System.DateTime.Now.AddMinutes(LOGINEXPIRESTIME); HttpContext.Current.Response.Cookies.Add(cookie); } return value; } public static string GetCookie(CookieKey key) { HttpCookie cookie = HttpContext.Current.Request.Cookies[key.ToString()]; if (cookie != null) { string result = HttpUtility.UrlDecode(cookie.Value); return result; } return string.Empty; } public static void ClearCookie(CookieKey key) { HttpCookie cookie = HttpContext.Current.Request.Cookies[key.ToString()]; if (cookie != null) { cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookie); } } }
时间: 2024-10-27 07:44:16