| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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);
- }
- /// <summary>
- /// 设置Cookie
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- 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);
- }
- /// <summary>
- /// 获取Cookie
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
|