123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /******************************************************************************
- * 作者: 季健国
- * 创建时间: 2012/6/6 22:19:49
- *
- *
- ******************************************************************************/
- using System;
- using System.Web;
- namespace Ant.Service.Common
- {
- /// <summary>
- /// Cookie辅助类
- /// </summary>
- public class CookieHelper
- {
- /// <summary>
- /// 清除指定Cookie
- /// </summary>
- /// <param name="cookiename">cookiename</param>
- public static void ClearCookie(string cookiename)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
- if (cookie != null)
- {
- cookie.Expires = DateTime.Now.AddDays(-14400);
- HttpContext.Current.Response.Cookies.Remove(cookiename);
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- }
- /// <summary>
- /// 获取指定Cookie值
- /// </summary>
- /// <param name="cookiename">cookiename</param>
- /// <returns></returns>
- public static string GetCookieValue(string cookiename)
- {
- HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
- string str = string.Empty;
- if (cookie != null)
- {
- str = cookie.Value;
- }
- return str;
- }
- /// <summary>
- /// 获取cookie
- /// </summary>
- /// <param name="cookiename"></param>
- /// <returns></returns>
- public static HttpCookie GetCookie(string cookiename)
- {
- return HttpContext.Current.Request.Cookies[cookiename];
- }
- /// <summary>
- /// 添加一个Cookie,默认浏览器关闭过期
- /// </summary>
- public static void SetCookie(string cookiename, System.Collections.Specialized.NameValueCollection cookievalue,int? days)
- {
- var cookie = HttpContext.Current.Request.Cookies[cookiename];
- if (cookie == null)
- {
- cookie = new HttpCookie(cookiename);
- cookie.Values.Add(cookievalue);
- var siteurl = System.Configuration.ConfigurationManager.AppSettings["siteurl"];
- if (!string.IsNullOrEmpty(siteurl))
- {
- cookie.Domain = siteurl.Replace("www.", "");
- }
- if (days != null && days > 0) { cookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(days)); }
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- else
- {
- HttpContext.Current.Response.SetCookie(cookie);
- }
- }
- /// <summary>
- /// 添加一个Cookie
- /// </summary>
- /// <param name="cookiename">cookie名</param>
- /// <param name="cookievalue">cookie值</param>
- /// <param name="expires">过期时间 null为浏览器过期</param>
- public static void SetCookie(string cookiename, string cookievalue, int? expires)
- {
- var cookie = HttpContext.Current.Request.Cookies[cookiename];
- if (cookie == null)
- {
- cookie = new HttpCookie(cookiename);
- cookie.Value = cookievalue;
- var siteurl = System.Configuration.ConfigurationManager.AppSettings["siteurl"];
- if (!string.IsNullOrEmpty(siteurl))
- {
- cookie.Domain = siteurl.Replace("www.", "");
- }
- if (expires != null && expires > 0) { cookie.Expires = DateTime.Now.AddDays(Convert.ToInt32(expires)); }
- HttpContext.Current.Response.AppendCookie(cookie);
- }
- else
- {
- HttpContext.Current.Response.SetCookie(cookie);
- }
- }
- }
- }
|