cookie

存入cookie

if (Request.Cookies.AllKeys.Contains(loginInfo))
{
//如果cookie存在,更新cookie信息
HttpCookie hc = Request.Cookies[loginInfo];
hc.Value = Base64Helper.ToBase64Str(JsonConvert.SerializeObject(admin));
hc.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(hc);
}
else
{
//如果cookie不存在,创建新的cookie信息
HttpCookie hc = new HttpCookie(loginInfo, Base64Helper.ToBase64Str(JsonConvert.SerializeObject(admin)));
hc.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(hc);
}

清除登录cookie

/// <summary>
/// 清除登陆cookie信息
/// </summary>
public void ClearLoginCookie()
{
Session.Clear();
foreach (var item in Request.Cookies.AllKeys)
{
if (item == loginInfo)
{
HttpCookie hc = Response.Cookies[item];
hc.Expires = DateTime.Now.AddYears(-30);
Response.Cookies.Add(hc);
}
}
}

#region 清除所有cookie信息
/// <summary>
/// 清除所有cookie信息
/// </summary>
public void ClearAllCookie()
{
Session.Clear();
foreach (var item in Request.Cookies.AllKeys)
{
Response.Cookies[item].Expires.AddYears(-30);
}
}
#endregion

 

posted @ 2018-07-19 11:10  AZRNG  阅读(130)  评论(0编辑  收藏  举报