/// <summary>
/// 保存Cookie
/// </summary>
/// <param name="key">Cookie的键名称</param>
/// <param name="value">要保存的Cookie值</param>
protected void StoreCookie(string key, string value)
{
try
{
HttpCookie objCookie = new HttpCookie(key);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(value, false, 30);
objCookie.Value = FormsAuthentication.Encrypt(ticket);
ticket = null;
objCookie.Path = "/";
objCookie.Expires = DateTime.Now.AddDays(7);//保存7天
Response.Cookies.Add(objCookie);
objCookie = null;
}
catch { }
}
/// <summary>
/// 读取保存的Cookie值
/// </summary>
/// <param name="key">Cookie的键名称</param>
protected string GetCookieValue(string key)
{
if (Request.Cookies.Get(key) != null && !string.IsNullOrEmpty(Request.Cookies.Get(key).Value))
{
FormsAuthenticationTicket obj = null;
try
{
obj = FormsAuthentication.Decrypt(Request.Cookies.Get(key).Value);
return obj.Name;
}
catch { return string.Empty; }
finally { obj = null; }
}
return string.Empty;
}