using System; using System.Web; namespace SiteCore { public class WebCookie { private const string ViewCookieName = "ViewCookie"; public static void CookieLogin(string username, string ticket, int expiresDay) { CookieLogin(username, ticket, 0, expiresDay); } //登录 public static void CookieLogin(string username, string ticket, int type, int expiresDay) { HttpCookie cookie = new HttpCookie(webConfig.CookieName); cookie.Values.Clear(); cookie.Values.Add("User", username); cookie.Values.Add("Ticket", ticket); cookie.Values.Add("ST", type.ToString()); cookie.Path = "/"; cookie.Domain = webConfig.SiteDomain; if (expiresDay > 0) cookie.Expires = DateTime.Now.AddDays(expiresDay); HttpContext.Current.Response.Cookies.Add(cookie); //清队缓存 WebUser.RemoveUserCache(username); } //退出 public static void CookieLogout() { HttpCookie cookie = HttpContext.Current.Request.Cookies[webConfig.CookieName]; cookie.Domain = webConfig.SiteDomain; cookie.Path = "/"; cookie.Expires = DateTime.Now.AddDays(-1d); HttpContext.Current.Response.Cookies.Add(cookie); } public static string GetViewCookies(string key) { HttpCookie cookie = HttpContext.Current.Request.Cookies[ViewCookieName]; if (cookie != null) return cookie.Values[key]; return ""; } public static void SetViewCookies(string key, string value) { HttpCookie cookie = HttpContext.Current.Request.Cookies[ViewCookieName]; if (cookie == null) { cookie = new HttpCookie(ViewCookieName); cookie.Expires = DateTime.Now.AddDays(1); } if (!string.IsNullOrEmpty(cookie.Values[key])) { cookie.Values[key] = value; } else { cookie.Values.Add(key, value); } HttpContext.Current.Response.Cookies.Set(cookie); } /// /// 设置Cookie /// /// /// public static void SetCookie(string key, string value) { HttpCookie cookie = HttpContext.Current.Request.Cookies[key]; if (cookie == null) cookie = new HttpCookie(key); cookie.Value = value; HttpContext.Current.Response.Cookies.Set(cookie); } /// /// 获取Cookie /// /// /// public static string GetCookie(string key) { HttpCookie cookie = HttpContext.Current.Request.Cookies[key]; if (cookie != null) return cookie.Value; return ""; } public static void DelCookie(string key) { if (key == "") return; HttpCookie cookie = new HttpCookie(key); cookie.Expires = DateTime.Now.AddDays(-1d); HttpContext.Current.Response.Cookies.Add(cookie); } } }