第一种:
.NET 清除Cookie
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename];
if (cookie != null)
{
cookie.Values.Clear();
SetUserCookieExpireTime(cookiename, -1);
cookie.Domain = _domain;
System.Web.HttpContext.Current.Response.Cookies.Set(cookie);
}
public static void SetUserCookieExpireTime(string key, int days)
{
System.Web.HttpContext.Current.Response.Cookies[key].Domain = _domain;
System.Web.HttpContext.Current.Response.Cookies[key].Path = _cookiepath;
System.Web.HttpContext.Current.Response.Cookies[key].Expires = DateTime.Now.AddDays(days);
}
.NET 添加/更新Cookie
public static void AddUserCookies(string key,string value, string cookiename, string domain)
{
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename];
if (cookie == null)
{
cookie = new HttpCookie(cookiename);
cookie.Domain = domain;
cookie.Path = _cookiepath;
cookie.Values.Add(key, value);
HttpContext.Current.Response.AppendCookie(cookie);
}
else
{
if (System.Web.HttpContext.Current.Request.Cookies[cookiename].Values[key] != null)
{
cookie.Values.Set(key, value);
}
else
{
cookie.Domain = domain;
cookie.Path = _cookiepath;
cookie.Values.Add(key, value);
HttpContext.Current.Response.AppendCookie(cookie);
}
}
}